[ Team LiB ] |
Recipe 9.1 Filling a DataSet AsynchronouslyProblemGiven some database queries that return large result sets and cause the calling application to be unresponsive, you need to make the application more responsive during the fill. SolutionCreate a background thread and use it to run a query to fill a DataSet. You can also fill a DataGrid object from a background thread. The sample code contains one event handler and two methods:
The C# code is shown in Example 9-1. Example 9-1. File: AsynchronousFillForm.cs// Namespaces, variables, and constants using System; using System.Configuration; using System.Threading; using System.Runtime.Remoting.Messaging; using System.Data; using System.Data.SqlClient; private delegate void BindDataSetToDataGridDelegate(DataSet ds); // 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 = "OrderID"; private const String ORDERDATE_FIELD = "OrderDate"; private bool isWorking = false; // . . . private delegate void BindDataSetToDataGridDelegate(DataSet ds); private void goButton_Click(object sender, System.EventArgs e) { // Check if the DataSet is already being filled. if (!isWorking) { isWorking = true; // Clear the data grid. resultDataGrid.DataSource = null; // Create and start a new thread to fill the DataSet. Thread thread = new Thread(new ThreadStart(AsyncFillDataSet)); thread.Start( ); } else { // DataSet already being filled. Display a message. statusTextBox.Text += "DataSet still filling . . . " + Environment.NewLine; } } private void AsyncFillDataSet( ) { statusTextBox.Text = "Filling DataSet . . . " + Environment.NewLine; DataSet ds = new DataSet("Source"); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM Orders", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.FillSchema(orderTable, SchemaType.Source); da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM [Order Details]", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.FillSchema(orderDetailTable, SchemaType.Source); da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); statusTextBox.Text += "DataSet Fill complete." + Environment.NewLine; // Call the BindDataSetToDataGrid method asynchronously // on the Form's thread. this.BeginInvoke( new BindDataSetToDataGridDelegate(BindDataSetToDataGrid), new object[] {ds}); // Set flag indicating that the async fill is complete. isWorking = false; } private void BindDataSetToDataGrid(DataSet ds) { // Bind the default view of the Orders table to the data grid. resultDataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView; } DiscussionWhen a synchronous call is made, the caller thread is blocked until the call completes. An asynchronous call returns immediately, freeing the calling thread to continue with its work while a new thread is created to run the method in parallel. A new instance of a Thread is initialized using a constructor that takes a ThreadStart delegate argument, which references the method to be executed when the Thread starts executing. The Start( ) method of the Thread changes the state of the Thread to ThreadState.Running allowing the operating system to schedule it for execution. Once it begins executing, the ThreadStart delegate supplied in the Thread constructor invokes its method. Windows Form or control methods, such as a DataGrid, cannot be called on any thread other than the one that created the form or control because they are based on a single-threaded apartment (STA) model. Method calls from other threads must be marshaled to the creation thread. This can be done asynchronously by calling the BeginInvoke( ) method of the form, forcing the method to be executed on the thread that created the form or control. |
[ Team LiB ] |