C#マルチスレッドの使用依頼による非同期プログラミング


.NET Frameworkでは、メソッドを非同期で呼び出すことができます.このため、呼び出す方法と同じ署名を持つ依頼を定義する必要があります.公開言語の実行時には、適切な署名が自動的に使用され、この依頼に対してBeginInvokeメソッドとEndInvokeメソッドが定義されます.
注意:NET Compact Frameworkでは非同期依頼呼び出し、すなわちBeginInvokeおよびEndInvokeメソッドはサポートされていません.
BeginInvokeメソッドは非同期呼び出しを開始します.この方法は、非同期で実行する必要がある方法と同じパラメータと、他の2つのオプションパラメータを有します.最初のパラメータは、非同期呼び出しが完了したときに呼び出すメソッドを参照するAsyncCallback委任です.2番目のパラメータは、コールバックメソッドに情報を渡すユーザー定義オブジェクトです.BeginInvokeは、非同期呼び出しの完了を待たずにすぐに戻ります.BeginInvokeは、非同期呼び出しの進行状況を監視するために使用できるIAsyncResultを返します.
EndInvokeメソッドは、非同期呼び出しの結果を取得します.このメソッドは、BeginInvokeを呼び出した後、いつでも呼び出すことができます.非同期呼び出しがまだ完了していない場合、EndInvokeは、非同期呼び出しが完了するまで呼び出しスレッドをブロックします.EndInvokeパラメータには、非同期実行メソッドのoutおよびrefパラメータ、およびBeginInvokeによって返されるIAsyncResultが含まれます.
このトピックのコード例では、BeginInvokeとEndInvokeを使用して非同期呼び出しを行うための4つの一般的な方法を示します.BeginInvokeを呼び出すと、次の操作を実行できます.
  • は、いくつかの操作を行い、その後、呼び出しが完了するまでEndInvokeを呼び出す.
  • IAsyncResultを使用する.AsyncWaitHandleプロパティはWaitHandleを取得し、WaitHandleが信号を受信するまでWaitOneメソッドを使用して実行を阻止し、EndInvokeを呼び出します.
  • BeginInvokeによって返されたIAsyncResultをポーリングし、非同期呼び出しがいつ完了するかを決定し、EndInvokeを呼び出す.
  • は、コールバック方法のための依頼をBeginInvokeに渡す.非同期呼び出しが完了すると、このメソッドはThreadPoolスレッド上で実行されます.コールバックメソッドはEndInvokeを呼び出します.

  • 注意:どのような方法を使用しても、EndInvokeを呼び出して非同期呼び出しを完了します.
    テストメソッドと非同期委任の定義
    次のコード例では、同じ長時間実行を非同期で呼び出す方法TestMethodの様々な方法を示します.TestMethodメソッドには、処理が開始され、数秒休止して終了したことを示すコンソールメッセージが表示されます.TestMethodには、BeginInvokeとEndInvokeの署名に追加する方法を説明するoutパラメータがあります.refパラメータを同じように処理できます.
    次のコード例では、TestMethodの定義とAsyncMethodCallerという名前のTestMethodを非同期で呼び出すために使用できる依頼を示します.コード例をコンパイルするには、TestMethodの定義とAsyncMethodCallerの委任を含める必要があります.
    using System;
    using System.Threading; 
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncDemo 
        {
            // The method to be executed asynchronously.
            public string TestMethod(int callDuration, out int threadId) 
            {
                Console.WriteLine("Test method begins.");
                Thread.Sleep(callDuration);
                threadId = Thread.CurrentThread.ManagedThreadId;
                return String.Format("My call time was {0}.", callDuration.ToString());
            }
        }
        // The delegate must have the same signature as the method
        // it will call asynchronously.
        public delegate string AsyncMethodCaller(int callDuration, out int threadId);
    }

    EndInvokeを使用して非同期呼び出しを待つ
    非同期実行メソッドの最も簡単な方法は、委任されたBeginInvokeメソッドを呼び出すことによってメソッドの実行を開始し、メインスレッド上でいくつかの操作を実行し、委任されたEndInvokeメソッドを呼び出すことです.EndInvokeは、非同期呼び出しが完了するまで返されないため、呼び出しスレッドをブロックする可能性があります.この方法は、ファイルまたはネットワーク操作を実行するのに非常に適しています.
    注意:
    EndInvokeはブロックされる可能性があるため、ユーザインタフェースにサービスするスレッドからこのメソッドを呼び出すべきではありません.
    using System;
    using System.Threading;
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncMain 
        {
            public static void Main() 
            {
                // The asynchronous method puts the thread id here.
                int threadId;
    
                // Create an instance of the test class.
                AsyncDemo ad = new AsyncDemo();
    
                // Create the delegate.
                AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
    
                // Initiate the asychronous call.
                IAsyncResult result = caller.BeginInvoke(3000, 
                    out threadId, null, null);
    
                Thread.Sleep(0);
                Console.WriteLine("Main thread {0} does some work.",
                    Thread.CurrentThread.ManagedThreadId);
    
                // Call EndInvoke to wait for the asynchronous call to complete,
                // and to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, result);
    
                Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                    threadId, returnValue);
            }
        }
    }
    
    /* This example produces output similar to the following:
    
    Main thread 1 does some work.
    Test method begins.
    The call executed on thread 3, with return value "My call time was 3000.".
     */

    WaitHandleを使用して非同期呼び出しを待つ
    BeginInvokeから返されたIAsyncResultのAsyncWaitHandleプロパティを使用してWaitHandleを取得できます.非同期呼び出しが完了すると、WaitHandleは信号を受信し、WaitOneメソッドを呼び出して待つことができます.
    WaitHandleを使用する場合は、非同期呼び出しが完了する前または後になりますが、EndInvokeを呼び出して結果を取得する前に、他の処理を実行することもできます.
    注意:EndInvokeを呼び出すと、待機ハンドルは自動的に閉じません.待機ハンドルへのすべての参照を解放すると、ゴミ回収機能が待機ハンドルを回収すると、システムリソースが解放されます.ハンドルの使用が完了するとすぐにシステムリソースを解放するには、WaitHandleを呼び出します.Closeメソッドは、待機ハンドルを解放します.放出可能なオブジェクトを明示的に解放すると、ゴミ回収の作業効率が向上します.
    using System;
    using System.Threading;
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncMain 
        {
            static void Main() 
            {
                // The asynchronous method puts the thread id here.
                int threadId;
    
                // Create an instance of the test class.
                AsyncDemo ad = new AsyncDemo();
    
                // Create the delegate.
                AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
    
                // Initiate the asychronous call.
                IAsyncResult result = caller.BeginInvoke(3000, 
                    out threadId, null, null);
    
                Thread.Sleep(0);
                Console.WriteLine("Main thread {0} does some work.",
                    Thread.CurrentThread.ManagedThreadId);
    
                // Wait for the WaitHandle to become signaled.
                result.AsyncWaitHandle.WaitOne();
    
                // Perform additional processing here.
                // Call EndInvoke to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, result);
    
                // Close the wait handle.
                result.AsyncWaitHandle.Close();
    
                Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                    threadId, returnValue);
            }
        }
    }
    
    /* This example produces output similar to the following:
    
    Main thread 1 does some work.
    Test method begins.
    The call executed on thread 3, with return value "My call time was 3000.".
     */

    ポーリング非同期呼び出し完了
    BeginInvokeから返されたIAsyncResultのIsCompletedプロパティを使用して、非同期呼び出しがいつ完了するかを確認できます.この操作は、ユーザインタフェースのサービススレッドから非同期呼び出しを行う場合に実行できます.ポーリングが完了すると、呼び出しスレッドは、非同期呼び出しがThreadPoolスレッド上で実行されたときに実行され続けます.
    using System;
    using System.Threading;
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncMain 
        {
            static void Main() {
                // The asynchronous method puts the thread id here.
                int threadId;
    
                // Create an instance of the test class.
                AsyncDemo ad = new AsyncDemo();
    
                // Create the delegate.
                AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
    
                // Initiate the asychronous call.
                IAsyncResult result = caller.BeginInvoke(3000, 
                    out threadId, null, null);
    
                // Poll while simulating work.
                while(result.IsCompleted == false) {
                    Thread.Sleep(250);
                    Console.Write(".");
                }
    
                // Call EndInvoke to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, result);
    
                Console.WriteLine("
    The call executed on thread {0}, with return value \"{1}\".", threadId, returnValue); } } } /* This example produces output similar to the following: Test method begins. ............. The call executed on thread 3, with return value "My call time was 3000.". */

    非同期呼び出し完了時のコールバックメソッドの実行
    非同期呼び出しを開始するスレッドが結果を処理するスレッドである必要がない場合は、呼び出しが完了したときにコールバックメソッドを実行できます.コールバックメソッドはThreadPoolスレッド上で実行されます.
    コールバックメソッドを使用するには、コールバックメソッドを表すAsyncCallback依頼をBeginInvokeに渡す必要があります.コールバックメソッドで使用する情報を含むオブジェクトを渡すこともできます.コールバック・メソッドでは、IAsyncResult(コールバック・メソッドの一意のパラメータ)を強制的にAsyncResultオブジェクトに変換できます.その後、AsyncResult.AsyncDelegateプロパティを使用して、EndInvokeを呼び出すことができるように、コールバック・メソッドを起動するための委任を取得できます.
    例の説明:TestMethodのthreadIdパラメータはoutパラメータであるため、TestMethodはこのパラメータの入力値を使用しない.仮想変数がBeginInvoke呼び出しに渡されます.threadIdパラメータがrefパラメータの場合)、この変数はクラスレベルフィールドである必要があります.これにより、BeginInvokeとEndInvokeに同時に渡すことができます.
    BeginInvokeに渡されるステータス情報は、出力メッセージのフォーマットにコールバックメソッドを使用するフォーマット文字列です.タイプObjectとして渡されるため、ステータス情報は強制的に正しいタイプに変換されなければ使用できません.
    コールバックはThreadPoolスレッドで実行されます.ThreadPoolスレッドはバックグラウンドスレッドであり、これらのスレッドはプライマリスレッドが終了した後もアプリケーションの実行を維持しないため、サンプルのプライマリスレッドはコールバックが完了するのに十分な時間休止する必要があります.
    using System;
    using System.Threading;
    using System.Runtime.Remoting.Messaging;
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncMain 
        {
            static void Main() 
            {
                // Create an instance of the test class.
                AsyncDemo ad = new AsyncDemo();
    
                // Create the delegate.
                AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
    
                // The threadId parameter of TestMethod is an out parameter, so
                // its input value is never used by TestMethod. Therefore, a dummy
                // variable can be passed to the BeginInvoke call. If the threadId
                // parameter were a ref parameter, it would have to be a class-
                // level field so that it could be passed to both BeginInvoke and 
                // EndInvoke.
                int dummy = 0;
    
                // Initiate the asynchronous call, passing three seconds (3000 ms)
                // for the callDuration parameter of TestMethod; a dummy variable 
                // for the out parameter (threadId); the callback delegate; and
                // state information that can be retrieved by the callback method.
                // In this case, the state information is a string that can be used
                // to format a console message.
                IAsyncResult result = caller.BeginInvoke(3000,
                    out dummy, 
                    new AsyncCallback(CallbackMethod),
                    "The call executed on thread {0}, with return value \"{1}\".");
    
                Console.WriteLine("The main thread {0} continues to execute...", 
                    Thread.CurrentThread.ManagedThreadId);
    
                // The callback is made on a ThreadPool thread. ThreadPool threads
                // are background threads, which do not keep the application running
                // if the main thread ends. Comment out the next line to demonstrate
                // this.
                Thread.Sleep(4000);
    
                Console.WriteLine("The main thread ends.");
            }
    
            // The callback method must have the same signature as the
            // AsyncCallback delegate.
            static void CallbackMethod(IAsyncResult ar) 
            {
                // Retrieve the delegate.
                AsyncResult result = (AsyncResult) ar;
                AsyncMethodCaller caller = (AsyncMethodCaller) result.AsyncDelegate;
    
                // Retrieve the format string that was passed as state 
                // information.
                string formatString = (string) ar.AsyncState;
    
                // Define a variable to receive the value of the out parameter.
                // If the parameter were ref rather than out then it would have to
                // be a class-level field so it could also be passed to BeginInvoke.
                int threadId = 0;
    
                // Call EndInvoke to retrieve the results.
                string returnValue = caller.EndInvoke(out threadId, ar);
    
                // Use the format string to format the output message.
                Console.WriteLine(formatString, threadId, returnValue);
            }
        }
    }
    
    /* This example produces output similar to the following:
    
    The main thread 1 continues to execute...
    Test method begins.
    The call executed on thread 3, with return value "My call time was 3000.".
    The main thread ends.
     */
    原文:Calling Synchronous Methods Asynchronously
    転載を歓迎します.文章の出所を明記してください.http://blog.csdn.net/johnsonblog/article/details/39617473
    みんなの1つの健康なネット環境を返して、あなたと私から始めます
    THE END