以結束非同步作業的方式封鎖應用程式執行
無法在等候非同步作業的結果時繼續執行其他工作的應用程式必須封鎖,直到作業完成為止。 請使用下列其中一個選項,在等候非同步作業完成時,封鎖應用程式的主執行緒:
呼叫非同步作業的 EndOperationName 方法。 此主題中會示範這個方法。
使用非同步作業的 BeginOperationName 方法所傳回之 IAsyncResult 的 AsyncWaitHandle 屬性。 如需進行這個方法的範例,請參閱使用 AsyncWaitHandle 封鎖應用程式執行。
使用 EndOperationName 方法來封鎖 (直到非同步作業完成) 的應用程式,通常會先呼叫 BeginOperationName 方法,再執行不需要作業結果即可完成的所有工作,然後呼叫 EndOperationName。
範例
下列程式碼範例會示範使用 Dns 類別中的非同步方法,以擷取使用者指定之電腦的網域名稱系統資訊。 請注意,null (在 Visual Basic 中為 Nothing) 會傳遞給 BeginGetHostByName requestCallback 和 stateObject 參數,因為在使用這個方法時,並不需要這些引數。
' The following example demonstrates using asynchronous methods to
' get Domain Name System information for the specified host computer.
Imports System
Imports System.Net
Imports System.Net.Sockets
Namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class BlockUntilOperationCompletes
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.
' This example does not use a delegate or user-supplied object
' so the last two arguments are Nothing.
Dim result as IAsyncResult = Dns.BeginGetHostEntry(args(0), Nothing, Nothing)
Console.WriteLine("Processing your request for information...")
' Do any additional work that can be done here.
Try
' EndGetHostByName blocks until the process completes.
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
/*
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;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class BlockUntilOperationCompletes
{
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.
// This example does not use a delegate or user-supplied object
// so the last two arguments are null.
IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null);
Console.WriteLine("Processing your request for information...");
// Do any additional work that can be done here.
try
{
// EndGetHostByName blocks until the process completes.
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("{0}", aliases[i]);
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses");
for (int i = 0; i < addresses.Length; i++)
{
Console.WriteLine("{0}",addresses[i].ToString());
}
}
}
catch (SocketException e)
{
Console.WriteLine("An exception occurred while processing the request: {0}", e.Message);
}
}
}
}