閱讀英文

共用方式為


IAsyncResult 介面

定義

代表非同步作業的狀態。

C#
public interface IAsyncResult
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IAsyncResult
衍生
屬性

範例

下列範例示範如何使用 AsyncWaitHandle 屬性來取得 WaitHandle ,以及如何等候委派上的非同步呼叫。 非同步呼叫完成時, WaitHandle 會收到信號,且您可以呼叫 WaitOne 方法來等候它。

此範例包含兩個類別:包含非同步呼叫之方法的 類別,以及包含進行呼叫之方法的 Main 類別。

如需使用委派以非同步方式呼叫方法的詳細資訊和更多範例,請參閱 以非同步方式呼叫同步方法

C#
using System;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncDemo
    {
        // The method to be executed asynchronously.
        public string TestMethod(int callDuration, out int threadId)
        {
            Console.WriteLine("Test method begins.");
            Thread.Sleep(callDuration);
            threadId = Thread.CurrentThread.ManagedThreadId;
            return String.Format("My call time was {0}.", callDuration.ToString());
        }
    }
    // The delegate must have the same signature as the method
    // it will call asynchronously.
    public delegate string AsyncMethodCaller(int callDuration, out int threadId);
}
C#
using System;
using System.Threading;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class AsyncMain
    {
        static void Main()
        {
            // The asynchronous method puts the thread id here.
            int threadId;

            // Create an instance of the test class.
            AsyncDemo ad = new AsyncDemo();

            // Create the delegate.
            AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);

            // Initiate the asychronous call.
            IAsyncResult result = caller.BeginInvoke(3000,
                out threadId, null, null);

            Thread.Sleep(0);
            Console.WriteLine("Main thread {0} does some work.",
                Thread.CurrentThread.ManagedThreadId);

            // Wait for the WaitHandle to become signaled.
            result.AsyncWaitHandle.WaitOne();

            // Perform additional processing here.
            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            // Close the wait handle.
            result.AsyncWaitHandle.Close();

            Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
                threadId, returnValue);
        }
    }
}

/* This example produces output similar to the following:

Main thread 1 does some work.
Test method begins.
The call executed on thread 3, with return value "My call time was 3000.".
 */

備註

IAsyncResult介面是由包含可以非同步作業之方法的類別所實作。 它是起始非同步作業的方法傳回類型,例如 FileStream.BeginRead ,而且會傳遞至結束非同步作業的方法,例如 FileStream.EndReadIAsyncResult 當非同步作業完成時,物件也會傳遞至委派所 AsyncCallback 叫用的方法。

支援 IAsyncResult 介面的物件會儲存非同步作業的狀態資訊,並提供同步處理物件,以允許作業完成時發出執行緒的訊號。

注意

類別是 當您 AsyncResult 使用委派以非同步方式呼叫方法時,方法所 BeginInvoke 傳回的 IAsyncResult 實作。

如需介面使用方式 IAsyncResult 的詳細描述,請參閱 以非同步方式呼叫同步方法 主題。

屬性

AsyncState

取得使用者定義的物件,這個物件符合或包含非同步作業的相關資訊。

AsyncWaitHandle

取得 WaitHandle,用來等候非同步作業完成。

CompletedSynchronously

取得值,這個值表示非同步作業是否同步完成。

IsCompleted

取得值,這個值表示非同步作業是否完成。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0