次の方法で共有


AsyncWaitHandle の使用によるアプリケーション実行のブロック

更新 : 2007 年 11 月

非同期操作の結果を待っている間に他の作業を行うことができないアプリケーションは、操作が完了するまでブロックする必要があります。非同期操作の完了を待っている間に、アプリケーションのメイン スレッドをブロックするには、次のいずれかのオプションを使用します。

1 つ以上の WaitHandle オブジェクトを使用して非同期操作が完了するまでブロックするアプリケーションでは、通常、BeginOperationName メソッドを呼び出した後、操作の結果がなくても行うことができる作業を実行してから、非同期操作が完了するまでブロックします。アプリケーションは、AsyncWaitHandle を使用して、いずれかの WaitOne メソッドを呼び出して単一の操作をブロックできます。一連の非同期操作の完了を待っている間ブロックするには、関連付けられている AsyncWaitHandle オブジェクトを配列内に格納し、いずれかの WaitAll メソッドを呼び出します。一連の非同期操作のいずれかの完了を待っている間ブロックするには、関連付けられている AsyncWaitHandle オブジェクトを配列内に格納し、いずれかの WaitAny メソッドを呼び出します。

サンプル

DNS クラスで非同期メソッドを使用して、ユーザー指定のコンピュータの DNS (Domain Name System) 情報を取得するコード例を次に示します。この例では、非同期操作に関連付けられている 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.

Imports System
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
/*
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("{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("Exception occurred while processing the request: {0}", 
                    e.Message);
            }
        }
    }
}

参照

概念

イベントベースの非同期パターンの概要

その他の技術情報

非同期プログラミングのデザイン パターン