非同期操作の終了によるアプリケーション実行のブロックBlocking Application Execution by Ending an Async Operation

非同期操作の結果の待機中に、他の作業を継続できないアプリケーションは、操作が完了するまでブロックする必要があります。Applications that cannot continue to do other work while waiting for the results of an asynchronous operation must block until the operation completes. 次のオプションのいずれかを使用して、非同期操作が完了するまでの待機中に、アプリケーションのメイン スレッドをブロックします。Use one of the following options to block your application's main thread while waiting for an asynchronous operation to complete:

非同期操作が完了するまでブロックするために EndOperationName メソッドを使用するアプリケーションは、通常は BeginOperationName メソッドを呼び出し、操作の結果なしで実行できる任意の作業を実行して、EndOperationName を呼び出します。Applications that use the EndOperationName method to block until an asynchronous operation is complete will typically call the BeginOperationName method, perform any work that can be done without the results of the operation, and then call EndOperationName.

Example

次のコード例は、ユーザー指定のコンピューターのドメイン ネーム システム情報を取得するために、Dns クラスの非同期メソッドを使用してデモを実行します。The following code example demonstrates using asynchronous methods in the Dns class to retrieve Domain Name System information for a user-specified computer. この方法を使用する場合はこれらの引数は必要ないため、null (Visual Basic の場合は Nothing) は、BeginGetHostByNamerequestCallbackstateObject パラメーターに渡されることに注意してください。Note that null (Nothing in Visual Basic) is passed for the BeginGetHostByNamerequestCallback and stateObject parameters because these arguments are not required when using this approach.

/*
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);
            }
        }
    }
}
' 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

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

関連項目See also