async (C# 參考)

async 修飾詞表示方法、修改它的 Lambda 運算式匿名方法 是非同步的。 這種方法稱為非同步方法。

可提供便利的方式進行可能需要長期執行的工作,而不封鎖呼叫端執行緒。 非同步方法的呼叫端可以繼續它的工作,而不需等待非同步方法。

注意事項注意事項

async 與 await 關鍵字在 Visual Studio 2012 中引入。如需在該版本的其他新功能的詳細資訊,請參閱 Visual Studio 2012 的新功能

如需非同步程式設計的簡介,請參閱 使用 Async 和 Await 設計非同步程式 (C# 和 Visual Basic)

下列範例會示範非同步事件處理常式,則為 StartButton_Click,結構呼叫非同步方法,則為 ExampleMethodAsync。 從方法的結果是下載網站的長度。 程式碼適用於 Windows Presentation Foundation (WPF) 或 Windows 市集應用程式。

// In desktop apps that you create by using Visual Studio 2012, you must 
// add a reference and a using directive for System.Net.Http.
// In Windows Store apps, you must add using directives for System.Net.Http 
// and System.Threading.Tasks.

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    // ExampleMethodAsync returns a Task<int> and has an int result.
    // A value is assigned to intTask when ExampleMethodAsync reaches
    // an await.
    try
    {
        Task<int> intTask = ExampleMethodAsync();
        // You can do other work here that doesn't require the result from
        // ExampleMethodAsync. . . .
        // You can access the int result when ExampleMethodAsync completes.
        int intResult = await intTask;
    
        // Or you can combine the previous two steps:
        //int intResult = await ExampleMethodAsync();

        // Process the result (intResult). . . .
    }
    catch (Exception)
    {
        // Process the exception. . . .
    }
}

public async Task<int> ExampleMethodAsync()
{
    var httpClient = new HttpClient();

    // At the await expression, execution in this method is suspended, and
    // control returns to the caller of ExampleMethodAsync.
    // Variable exampleInt is assigned a value when GetStringAsync completes.
    int exampleInt = (await httpClient.GetStringAsync("https://msdn.microsoft.com")).Length;

    // You can break the previous line into several steps to clarify what happens:
    //Task<string> contentsTask = httpClient.GetStringAsync("https://msdn.microsoft.com");
    //string contents = await contentsTask;
    //int exampleInt = contents.Length; 

    // Continue with whatever processing is waiting for exampleInt. . . .

    // After the return statement, any method that's awaiting
    // ExampleMethodAsync can get the integer result.
    return exampleInt;
}

重要

如需使用類似的項目的完整的範例,請參閱 逐步解說:使用 Async 和 Await 存取 Web (C# 和 Visual Basic)。您可以下載逐步解說程式碼從 開發人員程式碼範例

通常, async 關鍵字修改的方法包含至少一個 等候 運算式或陳述式。 方法會以同步方式執行,直到到達第一個 await 運算式,此時,它會暫止,直到等候的工作已完成。 同時,控制權回到方法的呼叫端。 如果方法不包含 await 運算式或陳述式,則它同步執行。 編譯器警告提醒您對不包含 await 所有的非同步方法,因為這種情況可能表示錯誤。 如需詳細資訊,請參閱編譯器警告 (層級 1) CS4014

async 關鍵字是內容關鍵字。 會修改方法、Lambda 運算式或匿名方法時,它是關鍵字。 在其他內容中,則會將它解譯為識別項。

傳回類型

非同步方法可能有 TaskTask<TResult>void的傳回型別。 方法不可以宣告任何 reft3c3bfhx(v=vs.110).md 參數,不過,它可以呼叫具有這類參數的方法。

如果方法的 傳回 陳述式指定型別參數,運算元您指定 Task<TResult> ,因為非同步方法的傳回型別。 您可以使用 Task ,如果有意義的值未傳回,當方法完成時。 也就是對方法的呼叫傳回,則為 Task,但是,當 Task 完成時,等候 Task 的任何 await 運算式評估為 null。

void 傳回型別主要用於定義事件處理常式,則需要 void 傳回型別。 方法所傳回的非同步方法的呼叫端不等候它無法攔截方法擲回的例外狀況。

如需詳細資訊與範例,請參閱非同步方法的傳回型別 (C# and Visual Basic)

請參閱

工作

逐步解說:使用 Async 和 Await 存取 Web (C# 和 Visual Basic)

參考

await (C# 參考)

AsyncStateMachineAttribute

概念

使用 Async 和 Await 設計非同步程式 (C# 和 Visual Basic)