非同步作業Asynchronous Operations

某些資料庫作業 (如命令執行) 要花費相當長的時間才能完成。Some database operations, such as command executions, can take significant time to complete. 在此情況下,單一執行緒應用程式必須封鎖其他作業並等待命令完成後,才能繼續它們自己的作業。In such a case, single-threaded applications must block other operations and wait for the command to finish before they can continue their own operations. 相反的,將長期執行作業指派給背景執行緒,可讓前景執行緒在作業過程中保持作用中狀態。In contrast, being able to assign the long-running operation to a background thread allows the foreground thread to remain active throughout the operation. 例如,若在 Windows 應用程式中將長期執行作業委派給背景執行緒,可讓使用者介面執行緒在作業執行時保持回應狀態。In a Windows application, for example, delegating the long-running operation to a background thread allows the user interface thread to remain responsive while the operation is executing.

.NET Framework 提供數個標準非同步設計模式,可供開發人員用於利用背景執行緒,讓使用者介面或高優先權執行緒可以完成其他作業。The .NET Framework provides several standard asynchronous design patterns that developers can use to take advantage of background threads and free the user interface or high-priority threads to complete other operations. ADO.NET 在其 SqlCommand 類別中,支援這些相同的設計模式。ADO.NET supports these same design patterns in its SqlCommand class. 尤其是,BeginExecuteNonQueryBeginExecuteReaderBeginExecuteXmlReader 方法,搭配 EndExecuteNonQueryEndExecuteReaderEndExecuteXmlReader 方法,都可以提供非同步支援。Specifically, the BeginExecuteNonQuery, BeginExecuteReader, and BeginExecuteXmlReader methods, paired with the EndExecuteNonQuery, EndExecuteReader, and EndExecuteXmlReader methods, provide the asynchronous support.

注意

非同步程式設計是 .NET Framework 的核心功能,而 ADO.NET 可充分利用這些標準設計模式。Asynchronous programming is a core feature of the .NET Framework, and ADO.NET takes full advantage of the standard design patterns. 如需開發人員可用之不同非同步技術的詳細資訊,請參閱以非同步方式呼叫同步方法For more information about the different asynchronous techniques available to developers, see Calling Synchronous Methods Asynchronously.

儘管與 ADO.NET 功能搭配使用非同步技術並不會有任何額外的特殊考量,但大多數的開發人員都只會在 ADO.NET 中使用非同步功能,而不會在 .NET Framework 中的其他領域使用。Although using asynchronous techniques with ADO.NET features does not add any special considerations, it is likely that more developers will use asynchronous features in ADO.NET than in other areas of the .NET Framework. 瞭解建立多執行緒應用程式的優缺點是很重要的。It is important to be aware of the benefits and pitfalls of creating multithreaded applications. 本節中接下來的範例會指出在建置包括多執行緒功能的應用程式時,開發人員需要考量的數個重要問題。The examples that follow in this section point out several important issues that developers will need to take into account when building applications that incorporate multithreaded functionality.

本節內容In This Section

使用回呼的 Windows 應用程式Windows Applications Using Callbacks
提供範例,示範如何安全地執行非同步命令,以及正確地處理單獨執行緒與表單及其內容之容的互動。Provides an example demonstrating how to execute an asynchronous command safely, correctly handling interaction with a form and its contents from a separate thread.

使用 Wait 控制代碼的 ASP.NET 應用程式ASP.NET Applications Using Wait Handles
提供範例,示範如何從 ASP.NET 網頁執行多個並行命令,並在所有命令完成後使用等候控制代碼管理作業。Provides an example demonstrating how to execute multiple concurrent commands from an ASP.NET page, using Wait handles to manage the operation at completion of all the commands.

在主控台應用程式中輪詢Polling in Console Applications
提供範例,示範如何使用輪詢等待從主控台應用程式執行的非同步命令完成。Provides an example demonstrating the use of polling to wait for the completion of an asynchronous command execution from a console application. 此技術在類別庫或不具使用者介面的其他應用程式中也有效。This technique is also valid in a class library or other application without a user interface.

另請參閱See also