在等待异步操作结果时无法继续执行其他工作的应用程序必须阻止,直到操作完成。 使用以下选项之一在等待异步操作完成时阻止应用程序的主线程:
使用异步操作BeginOperationName方法返回的IAsyncResult对象的AsyncWaitHandle属性。 本主题演示了此方法。
调用异步操作的 EndOperationName 方法。 有关演示此方法的示例,请参阅 通过结束异步操作来阻止应用程序执行。
使用一个或多个 WaitHandle 对象阻止的应用程序在异步操作完成之前通常会调用 BeginOperationName 方法,执行无需操作结果即可完成的任何工作,然后在异步操作完成之前阻止。 应用程序可以通过使用AsyncWaitHandle调用WaitOne中的一种方法来阻止单个操作。 若要在等待一组异步操作完成时阻止,请将关联的 AsyncWaitHandle 对象存储在数组中并调用其中 WaitAll 一种方法。 若要在等待一组异步操作完成时阻止,请将关联的 AsyncWaitHandle 对象存储在数组中并调用其中 WaitAny 一种方法。
示例
下面的代码示例演示如何使用 DNS 类中的异步方法检索用户指定的计算机的域名系统信息。 该示例演示了如何使用与异步操作关联的 WaitHandle 进行阻塞。 请注意,对于 BeginGetHostByNamerequestCallback 和 stateObject 参数,传递 null(在 Visual Basic 中为 Nothing),因为使用此方法时不需要这些参数。
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class WaitUntilOperationCompletes
{
public static void Main(string[] args)
{
// Make sure the caller supplied a host name.
if (args.Length == 0 || args[0].Length == 0)
{
// Print a message and exit.
Console.WriteLine("You must specify the name of a host computer.");
return;
}
// Start the asynchronous request for DNS information.
IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
Console.WriteLine("Processing request for information...");
// Wait until the operation completes.
result.AsyncWaitHandle.WaitOne();
// The operation completed. Process the results.
try
{
// Get the results.
IPHostEntry host = Dns.EndGetHostEntry(result);
string[] aliases = host.Aliases;
IPAddress[] addresses = host.AddressList;
if (aliases.Length > 0)
{
Console.WriteLine("Aliases");
for (int i = 0; i < aliases.Length; i++)
{
Console.WriteLine($"{aliases[i]}");
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses");
for (int i = 0; i < addresses.Length; i++)
{
Console.WriteLine($"{addresses[i].ToString()}");
}
}
}
catch (SocketException e)
{
Console.WriteLine($"Exception occurred while processing the request: {e.Message}");
}
}
}
}
' The following example demonstrates using asynchronous methods to
' get Domain Name System information for the specified host computer.
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class WaitUntilOperationCompletes
Public Shared Sub Main(args() as String)
' Make sure the caller supplied a host name.
If (args.Length = 0)
' Print a message and exit.
Console.WriteLine("You must specify the name of a host computer.")
End
End If
' Start the asynchronous request for DNS information.
Dim result as IAsyncResult = Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
Console.WriteLine("Processing request for information...")
' Wait until the operation completes.
result.AsyncWaitHandle.WaitOne()
' The operation completed. Process the results.
Try
' Get the results.
Dim host as IPHostEntry = Dns.EndGetHostEntry(result)
Dim aliases() as String = host.Aliases
Dim addresses() as IPAddress = host.AddressList
Dim i as Integer
If aliases.Length > 0
Console.WriteLine("Aliases")
For i = 0 To aliases.Length - 1
Console.WriteLine("{0}", aliases(i))
Next i
End If
If addresses.Length > 0
Console.WriteLine("Addresses")
For i = 0 To addresses.Length - 1
Console.WriteLine("{0}", addresses(i).ToString())
Next i
End If
Catch e as SocketException
Console.WriteLine("An exception occurred while processing the request: {0}" _
, e.Message)
End Try
End Sub
End Class
End Namespace