次の方法で共有


AsyncResult クラス

非同期デリゲートでの非同期操作の結果をカプセル化します。

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

System.Object
   System.Runtime.Remoting.Messaging.AsyncResult

Public Class AsyncResult
   Implements IAsyncResult, IMessageSink
[C#]
public class AsyncResult : IAsyncResult, IMessageSink
[C++]
public __gc class AsyncResult : public IAsyncResult, IMessageSink
[JScript]
public class AsyncResult implements IAsyncResult, IMessageSink

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

AsyncResult クラスは、非同期デリゲートと組み合わせて使用します。デリゲートの BeginInvoke メソッドから返された IAsyncResultAsyncResult にキャストできます。 AsyncResult には、非同期の呼び出しが行われたデリゲート オブジェクトを保持する AsyncDelegate プロパティがあります。

BeginInvoke と非同期デリゲートの詳細については、「 非同期デリゲート 」を参照してください。

使用例

[Visual Basic, C#, C++] 非同期デリゲートでの非同期操作の結果を取得するために、 AsyncResult クラスを使用する方法を次のコード例に示します。

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


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

End Class 'SampleSyncronized

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


Public Class AsyncResultSample
   
   ' 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  

   Public Shared Sub Main()
      
      Dim result As Integer
      Dim param As Integer
      
      ' Creates an instance of a context-bound type SampleSynchronized.
      Dim sampSyncObj As New SampleSyncronized()
      
      ' Checks whether the object is a proxy, since it is context-bound.
      If RemotingServices.IsTransparentProxy(sampSyncObj) Then
         Console.WriteLine("sampSyncObj is a proxy.")
      Else
         Console.WriteLine("sampSyncObj is NOT a proxy.")
      End If 

      param = 10
      
      Console.WriteLine("")
      Console.WriteLine("Making a synchronous call on the context-bound object:")
      
      result = sampSyncObj.Square(param)
      Console.Write("The result of calling sampSyncObj.Square with ")
      Console.WriteLine("{0} is {1}.", param, result)
      Console.WriteLine("")     

      Dim sampleDelegate As New SampSyncSqrDelegate(AddressOf sampSyncObj.Square)
      param = 8
      
      Console.WriteLine("Making a single asynchronous call on the context-bound object:")
      
      Dim ar1 As IAsyncResult = sampleDelegate.BeginInvoke(param, New AsyncCallback(AddressOf AsyncResultSample.MyCallback), param)
      
      Console.WriteLine("Waiting for the asynchronous call to complete...")
      Dim wh As WaitHandle = ar1.AsyncWaitHandle
      wh.WaitOne()
      
      Console.WriteLine("")
      Console.WriteLine("Waiting for the AsyncCallback to complete...")
      Thread.Sleep(1000)
   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 the Synchronization context attribute.
[Synchronization()]
public class SampleSyncronized : ContextBoundObject {

    // A method that does some work, and returns the square of the given number.
    public int Square(int i)  {

        Console.Write("The hash of the thread executing ");
        Console.WriteLine("SampleSyncronized.Square is: {0}", 
                             Thread.CurrentThread.GetHashCode());
        return i*i;
    }
}

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

public class AsyncResultSample {

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

    public static void Main() {

        int result;
        int param;

        // Creates an instance of a context-bound type SampleSynchronized.
        SampleSyncronized sampSyncObj = new SampleSyncronized();

        // Checks whether the object is a proxy, since it is context-bound.
        if (RemotingServices.IsTransparentProxy(sampSyncObj))
            Console.WriteLine("sampSyncObj is a proxy.");
        else
            Console.WriteLine("sampSyncObj is NOT a proxy.");

        param = 10;

        Console.WriteLine("");
        Console.WriteLine("Making a synchronous call on the context-bound object:");

        result = sampSyncObj.Square(param);
        Console.Write("The result of calling sampSyncObj.Square with ");
        Console.WriteLine("{0} is {1}.", param, result);
        Console.WriteLine("");

        SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate(sampSyncObj.Square);
        param = 8;

        Console.WriteLine("Making a single asynchronous call on the context-bound object:");

        IAsyncResult ar1 = sampleDelegate.BeginInvoke( param, 
                              new AsyncCallback(AsyncResultSample.MyCallback), 
                              param);

        Console.WriteLine("Waiting for the asynchronous call to complete...");
        WaitHandle wh = ar1.AsyncWaitHandle;
        wh.WaitOne();

        Console.WriteLine("");
        Console.WriteLine("Waiting for the AsyncCallback to complete...");
        Thread.Sleep(1000);
    }
}

[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 the Synchronization context attribute.
[Synchronization]
public __gc class SampleSyncronized : public ContextBoundObject 
{
    // A method that does some work, and returns the square of the given number.
public:
    int Square(int i) 
    {
        Console::Write(S"The hash of the thread executing ");
        Console::WriteLine(S"SampleSyncronized::Square is: {0}",
                    __box(Thread::CurrentThread->GetHashCode()));
        return i*i;
    }
};

// The async delegate used to call a method with this signature asynchronously.
public __delegate int SampSyncSqrDelegate(int i);

public __gc class AsyncResultSample 
{
    // 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));
    }
};

int main()
{
    int result;
    int param;

    // Creates an instance of a context-bound type SampleSynchronized.
    SampleSyncronized* sampSyncObj = new SampleSyncronized();

    // Checks whether the Object* is a proxy, since it is context-bound.
    if (RemotingServices::IsTransparentProxy(sampSyncObj))
        Console::WriteLine(S"sampSyncObj is a proxy.");
    else
        Console::WriteLine(S"sampSyncObj is NOT a proxy.");

    param = 10;

    Console::WriteLine(S"");
    Console::WriteLine(S"Making a synchronous call on the context-bound Object*:");

    result = sampSyncObj->Square(param);
    Console::Write(S"The result of calling sampSyncObj.Square with ");
    Console::WriteLine(S" {0} is {1}.", __box(param), __box(result));
    Console::WriteLine(S"");

    SampSyncSqrDelegate* sampleDelegate = new SampSyncSqrDelegate(sampSyncObj, SampleSyncronized::Square);
    param = 8;

    Console::WriteLine(S"Making a single asynchronous call on the context-bound Object*:");

    IAsyncResult* ar1 = sampleDelegate->BeginInvoke(param, 
        new AsyncCallback(0, AsyncResultSample::MyCallback), 
        __box(param));

    Console::WriteLine(S"Waiting for the asynchronous call to complete...");
    WaitHandle* wh = ar1->AsyncWaitHandle;
    wh->WaitOne();

    Console::WriteLine(S"");
    Console::WriteLine(S"Waiting for the AsyncCallback to complete...");
    Thread::Sleep(1000);

    return 0;
}

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

必要条件

名前空間: System.Runtime.Remoting.Messaging

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

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

参照

AsyncResult メンバ | System.Runtime.Remoting.Messaging 名前空間 | 非同期デリゲート