無法在等待非同步操作結果期間繼續執行其他工作的應用程式,必須阻塞直到操作完成。 請使用以下選項之一,在等待非同步操作完成時阻擋應用程式的主執行緒:
使用AsyncWaitHandle非同步操作的 IAsyncResultOperationName 方法所回傳的屬性。 本主題將示範此方法。
呼叫非同步操作的 EndOperationName 方法。 示範此方法的範例請參見「 透過結束非同步操作阻擋應用程式執行」。
使用一個或多個 WaitHandle 物件阻塞直到非同步操作完成的應用程式,通常會呼叫 BeginOperationName 方法,執行任何可在操作結果下完成的工作,然後阻塞直到非同步操作完成。 應用程式可透過呼叫WaitOne中的AsyncWaitHandle方法之一來阻塞單一操作。 要在等待一組非同步操作完成時封鎖,將相關 AsyncWaitHandle 物件儲存在陣列中並呼叫其中一個 WaitAll 方法。 若要在等待一組非同步操作完成時封鎖,將相關 AsyncWaitHandle 物件儲存在陣列中並呼叫其中一個 WaitAny 方法。
範例
以下程式碼範例展示了使用 DNS 類別中的非同步方法,為使用者指定的電腦擷取網域名稱系統資訊。 範例展示了使用與非同步操作相關聯的WaitHandle進行阻斷。 請注意 null ,Nothing (在 Visual Basic 中)會傳遞 BeginGetHostByNamerequestCallback 和 stateObject 參數,因為使用此方法時不需要這些參數。
/*
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
另請參閱
- 事件驅動非同步模式 (EAP)
- 事件為基礎的非同步模式概述