Browse DevX
Sign up for e-mail newsletters from DevX


Build Better UIs with the Asynchronous Support in .NET 2.0  : Page 2

Good user interfaces let users keep working as seamlessly as possible while an application performs long background processing tasks. While .NET 1.0 certainly simplified the process of launching and managing multiple threads in Windows applications, you had to write much of the infrastructure yourself. In contrast, .NET 2.0 adds direct framework support for asynchronously fetching data and performing background tasks.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Fetching Data Asynchronously
Author's Note: The code in this section was drawn from the DataBindingEx.sln solution and the SQL Server scripts in downloadable sample code.

Asynchronous processing for fetching data is one of the big gains in Whidbey, especially in Windows Forms applications. You'll see how easily this new functionality lets you add asynchronous processing to enhance the responsiveness of your applications.

The SqlCommand object provides several asynchronous call options. Each returns an IAsyncResult object. Table 1 shows their definitions:

Table 1. The SqlCommand object's familiar methods now provide several asynchronous processing options as overloads.





Function BeginExecuteReader( _ ByVal callback As AsyncCallback, _ ByVal stateObject As Object, _ ByVal behavior As CommandBehavior) _ As IAsyncResult Function BeginExecuteReader( _ ByVal callback As AsyncCallback, _ ByVal stateObject As Object) _ As IAsyncResult Function BeginExecuteReader( _ ByVal behavior As CommandBehavior) _ As IAsyncResult Function BeginExecuteReader() As IAsyncResult

Use this method to execute T-SQL statements and return a SqlDataReader.

Note: For asynchronous executions, return the SqlCommand in the AsyncState and perform the appropriate cast to get the return type.

Use the callback parameter to register a method to be called after the asynchronous call completes.

The stateObject parameter is the object that will be returned when you call IAsyncResult.AsyncState.

The behavior parameter is a standard CommandBehavior enumeration value just as you'd use in the current version of .NET


Function BeginExecuteNonQuery( _ ByVal callback As AsyncCallback, _ ByVal stateObject As Object) _ As IAsyncResult Function BeginExecuteNonQuery() _ As IAsyncResult

Use this to perform NonQuery operations and return the number of rows affected.

All the other overload parameters are the same as already discussed.


Function BeginExecuteXmlReader( _ ByVal callback As AsyncCallback, _ ByVal stateObject As Object) _ As IAsyncResult Function BeginExecuteXmlReader() _ As IAsyncResult

Use this overload to make an asynchronous query that returns an XmlReader object.

All the other overload parameters are the same as already discussed.

The sample code implements both asynchronous and synchronous models of execution to bind data into the DataGridView controls. The sample also explains how to bind the controls back from the worker thread and much more in terms of error handling, etc.

Figure 2. Default Form: The figure shows how the default form should look after adding the controls described in steps 1-4.
To start,
  1. Create a new Windows Forms solution and rename it as required.
  2. Drag two DataGridView controls on to the form and rename the top and bottom grids as "dgCustomerView" and "dgOrders".
  3. Add a status bar to provide status information.
  4. So you can compare the asynchronous and synchronous models, create two buttons to invoke the appropriate method calls. Your default form should look like Figure 2.
  5. Add a class and name it DataStore.vb. This class will return the same data in either asynchronous or synchronous mode. Here's the method for the asynchronous data fetch within the DataStore class.

Private connectionString As String = _ "Data SOurce=INBAXPLVHAR\VISHNU;" + _ "Initial Catalog=Employee;" + _ "Integrated security=yes;" + _ "Asynchronous Processing=true;" Public Function PopulateDataAsynchronously( _ ByVal query As String, ByVal callback As AsyncCallback) As IAsyncResult Dim conn As New SqlConnection(connectionString) Try conn.Open() Dim cmd As New SqlCommand(query, conn) Dim IAsync As IAsyncResult = _ cmd.BeginExecuteReader(callback, _ cmd, CommandBehavior.CloseConnection) Return IAsync Catch appex As Exception ' handle to close the connection If conn.State <> ConnectionState.Closed Then conn.Close() Throw New ApplicationException( _ "Error occured." + appex.Message, _ appex.InnerException) End Try End Function

Notice that the connection string in the preceding code sets the property Asynchronous Processing=true. This ensures that the data fetch can follow an asynchronous approach when using BeginExecute; otherwise, calling BeginExecute would throw an InvalidOperationException.

The sample simply designates the connection string as a string. Alternatively, you could opt for a more readable approach using a SqlConnectionStringBuilder instance as follows:

Dim connectionString As New SqlConnectionStringBuilder connectionString.AsynchronousProcessing = True connectionString.DataSource = "Inbaxplvhar\vishnu" connectionString.InitialCatalog = "Employee" connectionString.IntegratedSecurity = True Dim connstring As String = _ connectionString.ConnectionString Dim conn As New SqlConnection(connectionString) conn.Open()

Thanks for your registration, follow us on our social networks to keep up-to-date