IAsyncResult.AsyncWaitHandle 属性    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取用于等待异步操作完成的 WaitHandle。
public:
 property System::Threading::WaitHandle ^ AsyncWaitHandle { System::Threading::WaitHandle ^ get(); };public System.Threading.WaitHandle AsyncWaitHandle { get; }member this.AsyncWaitHandle : System.Threading.WaitHandlePublic ReadOnly Property AsyncWaitHandle As WaitHandle属性值
用于等待异步操作完成的 WaitHandle。
示例
下面的示例演示如何使用 AsyncWaitHandle 属性获取属性 WaitHandle,以及如何等待委托上的异步调用。 当异步调用完成时 WaitHandle 会收到信号,而你可以通过调用 WaitOne 方法来等待它。
该示例由两个类组成:包含异步调用的方法的类,以及包含 Main 调用方法的类。
有关使用委托异步调用方法的详细信息和更多示例,请参阅 异步调用同步方法。
using namespace System;
using namespace System::Threading;
using namespace System::Runtime::InteropServices; 
namespace Examples {
namespace AdvancedProgramming {
namespace AsynchronousOperations
{
    public ref class AsyncDemo 
    {
    public:
        // The method to be executed asynchronously.
        String^ TestMethod(int callDuration, [OutAttribute] int% threadId) 
        {
            Console::WriteLine("Test method begins.");
            Thread::Sleep(callDuration);
            threadId = Thread::CurrentThread->ManagedThreadId;
            return String::Format("My call time was {0}.", callDuration);
        }
    };
    // The delegate must have the same signature as the method
    // it will call asynchronously.
    public delegate String^ AsyncMethodCaller(int callDuration, [OutAttribute] int% threadId);
}}}
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);
}
Imports System.Threading
Imports System.Runtime.InteropServices 
Namespace Examples.AdvancedProgramming.AsynchronousOperations
    Public Class AsyncDemo 
        ' The method to be executed asynchronously.
        Public Function TestMethod(ByVal callDuration As Integer, _
                <Out> ByRef threadId As Integer) As String
            Console.WriteLine("Test method begins.")
            Thread.Sleep(callDuration)
            threadId = Thread.CurrentThread.ManagedThreadId()
            return String.Format("My call time was {0}.", callDuration.ToString())
        End Function
    End Class
    ' The delegate must have the same signature as the method
    ' it will call asynchronously.
    Public Delegate Function AsyncMethodCaller(ByVal callDuration As Integer, _
        <Out> ByRef threadId As Integer) As String
End Namespace
#using <TestMethod.dll>
using namespace System;
using namespace System::Threading;
using namespace Examples::AdvancedProgramming::AsynchronousOperations;
void main() 
{
    // The asynchronous method puts the thread id here.
    int threadId;
    // Create an instance of the test class.
    AsyncDemo^ ad = gcnew AsyncDemo();
    // Create the delegate.
    AsyncMethodCaller^ caller = gcnew AsyncMethodCaller(ad, &AsyncDemo::TestMethod);
       
    // Initiate the asychronous call.
    IAsyncResult^ result = caller->BeginInvoke(3000, 
        threadId, nullptr, nullptr);
    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(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.".
 */
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.".
 */
Imports System.Threading
Imports System.Runtime.InteropServices 
Namespace Examples.AdvancedProgramming.AsynchronousOperations
    Public Class AsyncMain 
        Shared Sub Main() 
            ' The asynchronous method puts the thread id here.
            Dim threadId As Integer
            ' Create an instance of the test class.
            Dim ad As New AsyncDemo()
            ' Create the delegate.
            Dim caller As New AsyncMethodCaller(AddressOf ad.TestMethod)
       
            ' Initiate the asynchronous call.
            Dim result As IAsyncResult = caller.BeginInvoke(3000, _
                threadId, Nothing, Nothing)
            Thread.Sleep(0)
            Console.WriteLine("Main thread {0} does some work.", _
                Thread.CurrentThread.ManagedThreadId)
            ' Perform additional processing here and then
            ' wait for the WaitHandle to be signaled.
            result.AsyncWaitHandle.WaitOne()
            ' Call EndInvoke to retrieve the results.
            Dim returnValue As String = caller.EndInvoke(threadId, result)
            ' Close the wait handle.
            result.AsyncWaitHandle.Close()
            Console.WriteLine("The call executed on thread {0}, with return value ""{1}"".", _
                threadId, returnValue)
        End Sub
    End Class
End Namespace
'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.".
注解
返回值允许客户端等待异步操作完成,而不是轮询 IsCompleted ,直到操作结束。 返回值可用于执行 WaitOne、 WaitAny或 WaitAll 操作。
公共语言运行时提供许多可等待的对象,例如ManualResetEventAutoResetEvent,和Mutex,所有这些对象都镜像 Win32 同步基元。
实施者说明
在读取属性之前,实现IAsyncResult的对象不需要创建WaitHandle。AsyncWaitHandle 它是实现者的选择 IAsyncResult 。 但是,如果实现者创建 AsyncWaitHandle,则实现者有责任向该实现者发出 WaitHandle 信号,以在适当的时间终止等待。 例如, AsyncResult 当异步调用的方法返回时,代表调用方终止等待。 创建后, AsyncWaitHandle 应保持活动状态,直到用户调用结束异步操作的方法。 此时,可以丢弃隐藏 AsyncWaitHandle 的对象。
调用方说明
等待操作完成 (的客户端,而不是轮询) 使用此属性获取要等待的同步对象。
注意:使用 BeginInvoke 委托的方法异步调用方法并从生成的 IAsyncResult等待句柄中获取等待句柄时,建议通过调用 Close() 该方法,立即关闭等待句柄。 如果只是释放对等待句柄的所有引用,则当垃圾回收回收等待句柄时,系统资源将释放,但在显式关闭或释放可释放对象时,垃圾回收会更高效。 有关更多信息,请参见 AsyncWaitHandle 属性。