次の方法で共有


IAsyncResult インターフェイス

非同期操作のステータスを表します。

この型のすべてのメンバの一覧については、IAsyncResult メンバ を参照してください。

Public Interface IAsyncResult
[C#]
public interface IAsyncResult
[C++]
public __gc __interface IAsyncResult
[JScript]
public interface IAsyncResult

IAsyncResult を実装するクラス

クラス 説明
AsyncResult 非同期デリゲートでの非同期操作の結果をカプセル化します。
WebClientAsyncResult 標準の非同期メソッド パターンを実装するために XML Web サービス プロキシが使用できるように、 IAsyncResult の実装を提供します。

解説

IAsyncResult インターフェイスは、非同期操作できるメソッドが格納されたクラスで実装します。これは FileStream.BeginRead などの非同期操作を開始するメソッドの戻り値の型を示すと同時に、 FileStream.EndRead などの非同期操作を終了させるメソッドの 3 番目のパラメータの型を示します。 IAsyncResult オブジェクトも、非同期操作が完了したときに、 AsyncCallback デリゲートによって呼び出されるメソッドに渡されます。

IAsyncResult インターフェイスをサポートするオブジェクトは、非同期操作のステータス情報を格納するほか、同期操作が完了したときにその旨をスレッドに通知できるようにする同期オブジェクトを提供します。

IAsyncResult インターフェイスの使用方法の詳細については、「 非同期プログラミングの概要 」を参照してください。

使用例

[Visual Basic, C#, C++] IAsyncResult を使用して非同期操作の戻り値を取得する方法を次のサンプルで示します。

 
' Asynchronous Callback method.
Public Shared Sub MyCallback(ar As IAsyncResult)
   ' Obtains the last parameter of the delegate call.
   Dim value As Integer = Convert.ToInt32(ar.AsyncState)
   
   ' Obtains return value from the delegate call using EndInvoke.
   Dim aResult As AsyncResult = CType(ar, AsyncResult)
   Dim temp As SampSyncSqrDelegate = CType(aResult.AsyncDelegate, SampSyncSqrDelegate)
   Dim result As Integer = temp.EndInvoke(ar)
   
   Console.Write("Simple.SomeMethod (AsyncCallback): Result of ")
   Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result)
End Sub 'MyCallback  

[C#] 
// Asynchronous Callback method.
public static void MyCallback(IAsyncResult ar) {

    // Obtains the last parameter of the delegate call.
    int value = Convert.ToInt32(ar.AsyncState);

    // Obtains return value from the delegate call using EndInvoke.
    AsyncResult aResult = (AsyncResult)ar;
    SampSyncSqrDelegate temp = (SampSyncSqrDelegate)aResult.AsyncDelegate;
    int result = temp.EndInvoke(ar);

    Console.Write("Simple.SomeMethod (AsyncCallback): Result of ");
    Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result);
}

[C++] 
// Asynchronous Callback method.
public:
static void MyCallback(IAsyncResult* ar) 
{
    // Obtains the last parameter of the delegate call.
    int value = Convert::ToInt32(ar->AsyncState);

    // Obtains return value from the delegate call using EndInvoke.
    AsyncResult* aResult = dynamic_cast<AsyncResult*>(ar);
    SampSyncSqrDelegate* temp = static_cast<SampSyncSqrDelegate*>(aResult->AsyncDelegate);
    int result = temp->EndInvoke(ar);

    Console::Write(S"Simple::SomeMethod (AsyncCallback): Result of ");
    Console::WriteLine(S" {0} in SampleSynchronized::Square is {1} ", __box(value), __box(result));
}

[Visual Basic, C#, C++] 非同期操作の完了を待機する方法を次のサンプルで示します。

 
Imports System
Imports System.Threading
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Contexts
Imports System.Runtime.Remoting.Messaging


'
' Context-Bound type with Synchronization Context Attribute
'
<Synchronization()> Public Class SampleSyncronized
   Inherits ContextBoundObject
   
   ' A method that does some work - returns the square of the given number
   Public Function Square(i As Integer) As Integer
      Console.Write("SampleSyncronized.Square called.  ")
      Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode())
      Return i * i
   End Function 'Square
End Class 'SampleSyncronized



'
' Async delegate used to call a method with this signature asynchronously
'
Delegate Function SampSyncSqrDelegate(i As Integer) As Integer


'Main sample class
Public Class AsyncResultSample
   
   Public Shared Sub Main()
      Dim callParameter As Integer = 0
      Dim callResult As Integer = 0
      
      'Create an instance of a context-bound type SampleSynchronized
      'Because SampleSynchronized is context-bound, the object sampSyncObj 
      'is a transparent proxy
      Dim sampSyncObj As New SampleSyncronized()
      
      
      'call the method synchronously
      Console.Write("Making a synchronous call on the object.  ")
      Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode())
      callParameter = 10
      callResult = sampSyncObj.Square(callParameter)
      Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult)
      Console.WriteLine("")
      Console.WriteLine("")
      
      
      'call the method asynchronously
      Console.Write("Making an asynchronous call on the object.  ")
      Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode())
      Dim sampleDelegate As New SampSyncSqrDelegate(AddressOf sampSyncObj.Square)
      callParameter = 17
      
      Dim aResult As IAsyncResult = sampleDelegate.BeginInvoke(callParameter, Nothing, Nothing)
      
      'Wait for the call to complete
      aResult.AsyncWaitHandle.WaitOne()
      
      callResult = sampleDelegate.EndInvoke(aResult)

      Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult)

   End Sub 'Main

End Class 'AsyncResultSample

[C#] 
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;

//
// Context-Bound type with Synchronization Context Attribute
//
[Synchronization()]
public class SampleSyncronized : ContextBoundObject
{
    // A method that does some work - returns the square of the given number
    public int Square(int i)
    {
        Console.Write("SampleSyncronized.Square called.  ");
        Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
        return i*i;
    }
}


//
// Async delegate used to call a method with this signature asynchronously
//
public delegate int SampSyncSqrDelegate(int i);

//Main sample class
public class AsyncResultSample
{
    public static void Main()
    {
        int callParameter = 0;
        int callResult = 0;

        //Create an instance of a context-bound type SampleSynchronized
        //Because SampleSynchronized is context-bound, the object sampSyncObj 
        //is a transparent proxy
        SampleSyncronized sampSyncObj = new SampleSyncronized();


        //call the method synchronously
        Console.Write("Making a synchronous call on the object.  ");
        Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
        callParameter = 10;
        callResult = sampSyncObj.Square(callParameter);
        Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult);


        //call the method asynchronously
        Console.Write("Making an asynchronous call on the object.  ");
        Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
        SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square);
        callParameter = 17;

        IAsyncResult aResult = sampleDelegate.BeginInvoke(callParameter, null, null);

        //Wait for the call to complete
        aResult.AsyncWaitHandle.WaitOne();

        callResult = sampleDelegate.EndInvoke(aResult);
        Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Contexts;
using namespace System::Runtime::Remoting::Messaging;

//
// Context-Bound type with Synchronization Context Attribute
//
public __gc class SampleSyncronized : public ContextBoundObject 
{
    // A method that does some work - returns the square of the given number
public:
    int Square(int i) 
    {
        Console::Write(S"SampleSyncronized::Square called.  ");
        Console::WriteLine(S"The hash of the current thread is: {0}", __box(Thread::CurrentThread->GetHashCode()));
        return i*i;
    }
};

//
// Async delegate used to call a method with this signature asynchronously
//
__delegate int SampSyncSqrDelegate(int i);

//Main sample class
int main() 
{
    int callParameter = 0;
    int callResult = 0;

    //Create an instance of a context-bound type SampleSynchronized
    //Because SampleSynchronized is context-bound, the Object* sampSyncObj 
    //is a transparent proxy
    SampleSyncronized* sampSyncObj = new SampleSyncronized();


    //call the method synchronously
    Console::Write(S"Making a synchronous call on the Object*.  ");
    Console::WriteLine(S"The hash of the current thread is: {0}", __box(Thread::CurrentThread->GetHashCode()));
    callParameter = 10;
    callResult = sampSyncObj->Square(callParameter);
    Console::WriteLine(S"Result of calling sampSyncObj.Square with {0} is {1}.\n\n", __box(callParameter), __box(callResult));


    //call the method asynchronously
    Console::Write(S"Making an asynchronous call on the Object*.  ");
    Console::WriteLine(S"The hash of the current thread is: {0}", __box(Thread::CurrentThread->GetHashCode()));
    SampSyncSqrDelegate* sampleDelegate = new SampSyncSqrDelegate(sampSyncObj, SampleSyncronized::Square);
    callParameter = 17;

    IAsyncResult* aResult = sampleDelegate->BeginInvoke(callParameter, 0, 0);

    //Wait for the call to complete
    aResult->AsyncWaitHandle->WaitOne();

    callResult = sampleDelegate->EndInvoke(aResult);
    Console::WriteLine(S"Result of calling sampSyncObj.Square with {0} is {1}.", __box(callParameter), __box(callResult));
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

IAsyncResult メンバ | System 名前空間