非同期操作の結果を待機している間に他の作業を実行できるアプリケーションは、操作が完了するまで待機をブロックしないでください。 次のいずれかのオプションを使用して、非同期操作の完了を待機しながら命令の実行を続行します。
非同期操作の IsCompletedIAsyncResult メソッドによって返されるの プロパティを使用して、操作が完了したかどうかを確認します。 この方法はポーリングと呼ばれ、このトピックで説明します。
AsyncCallback デリゲートを使用して、非同期操作の結果を別のスレッドで処理します。 この方法を示す例については、「 AsyncCallback デリゲートを使用した非同期操作の終了」を参照してください。
例
次のコード例では、 Dns クラスで非同期メソッドを使用して、ユーザー指定のコンピューターのドメイン ネーム システム情報を取得する方法を示します。 この例では、非同期操作を開始し、操作が完了するまでコンソールでピリオド (".") を出力します。 この方法を使用する場合、これらの引数は必要ないため、BeginGetHostByNameAsyncCallbackパラメーターと Object パラメーターには null (Visual Basic では Nothing) が渡されることに注意してください。
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
This example polls to detect the end of the asynchronous operation.
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class PollUntilOperationCompletes
{
static void UpdateUserInterface()
{
// Print a period to indicate that the application
// is still working on the request.
Console.Write(".");
}
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...");
// Poll for completion information.
// Print periods (".") until the operation completes.
while (result.IsCompleted != true)
{
UpdateUserInterface();
}
// The operation is complete. Process the results.
// Print a new line.
Console.WriteLine();
try
{
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($"An 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.
'This example polls to detect the end of the asynchronous operation.
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Namespace Examples.AdvancedProgramming.AsynchronousOperations
Public Class PollUntilOperationCompletes
Shared Sub UpdateUserInterface()
' Print a period to indicate that the application
' is still working on the request.
Console.Write(".")
End Sub
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...")
' Poll for completion information.
' Print periods (".") until the operation completes.
Do while result.IsCompleted <> True
UpdateUserInterface()
Loop
' The operation is complete. Process the results.
' Print a new line.
Console.WriteLine()
Try
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
こちらも参照ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET