Canceling an Asynchronous Query

[ Team LiB ] Recipe Canceling an Asynchronous Query Problem Given a query running that runs asynchronously on a background thread, you want to give the user the option to cancel the query if it is taking too long. Solution Abort the background thread and clean up in an exception handler. | Team LiB Recipe Canceling an Asynchronous Query Problem Given a query running that runs asynchronously on a background thread you want to give the user the option to cancel the query if it is taking too long. Solution Abort the background thread and clean up in an exception handler. The sample code contains two event handlers and a single method Start Checks whether there is an existing background thread loading the DataSet. If the DataSet is not being loaded a new thread is created invoking the AsyncFillDataSet method to fill a DataSet. Otherwise a message is displayed stating that the DataSet is currently being filled. Cancel Aborts the background thread filling the DataSet. AsyncFillDataSet This method loads a DataSet with the Orders and Order Details tables from the Northwind database. The method displays a message when the method has started and when it has completed. The method also traps the ThreadAbortException to handle the situation where the fill on the background thread is canceled. The C code is shown in Example 9-2. Example 9-2. File Namespaces variables and constants using System using using using using Table name constants private const String ORDERS_TABLE Orders private const String ORDERDETAILS_TABLE OrderDetails Relation name constants private const String ORDERS_ORDERDETAILS_RELATION Orders_OrderDetails_Relation Field name constants private const String ORDERID_FIELD OrderlD private const String ORDERDATE_FIELD OrderDate private Thread thread . . . private void startButton_Click object sender e Check if a new thread can be created. if thread null 0 Create and start a new thread to fill the DataSet. thread new Thread new ThreadStart AsyncFillDataSet true else DataSet already being filled. Display a .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.