Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Asynchronous Processing
Message
From
20/11/2007 13:51:26
 
 
To
20/11/2007 13:35:16
General information
Forum:
ASP.NET
Category:
Databases
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01270151
Message ID:
01270235
Views:
30
This message has been marked as the solution to the initial question of the thread.
Thanks so much Kevin!

Here's the 411...

The SqlCommand object contains the following new member functions that you can use for asynchronous processing:

• BeginExecuteReader() and EndExecuteReader()
• BeginExecuteNonQuery() and EndExecuteNonQuery()
• BeginExecuteXmlReader() and EndExecuteXmlReader()

Visual Studio 2005 provides three methods for working with asynchronous commands:

1 Polling method: This requires using the new IsCompleted property.

2 The callback model: The developer specifies a function to be executed when the asynchronous command completes.
The BeginXXX() methods contain an overload for a delegate parameter.

3 Synchronization objects (the wait model): The IAsyncResult object listed previously
contains a WaitHandle property.


So...note the Begin and End prefixes on each of these methods. These all implement the new IAsyncResult interface in Visual Studio 2005. Any time you begin an asynchronous process using one of the methods just listed, the method returns an IAsyncResult object that represents the status of the asynchronous process.

// Basic Asynchronous Processing Using the Polling Method
SqlConnection oSqlConn = this.GetConnection(nDatabaseKey);
oSqlConn.Open();
SqlCommand oCmd = new SqlCommand("SELECT * FROM Orders", oSqlConn);
IAsyncResult oAR = oCmd.BeginExecuteReader();
while (oAR.IsCompleted == false) {
   // Do something while waiting
}
SqlDataReader r = oCmd.EndExecuteReader(oAR);
oMyConn.Close();
Example of callback method...
SqlConnection oSqlConn = this.GetConnection(nDatabaseKey);
oSqlConn.Open();
SqlCommand oCmd = new SqlCommand("SELECT * FROM Orders", oSqlConn);
AsyncCallback oCallBack = new AsyncCallback(GetResult);
oCmd.BeginExecuteReader(oCallBack, oCmd,CommandBehavior.CloseConnection);
...


public void GetResult(IAsyncResult result)
{
SqlCommand oCmd = (SqlCommand)result.AsyncState;
SqlDataReader rdr =oCmd.EndExecuteReader(result);
// Do something with the reader
}
Example of the wait model...
SqlConnection oSqlConn = this.GetConnection(nDatabaseKey);
oSqlConn.Open();
SqlCommand oCmd1 = new SqlCommand("SELECT * FROM Orders", oMyConn);
SqlCommand oCmd2 = new SqlCommand("SELECT * FROM shippers", oMyConn);
WaitHandle[] oHandles = new WaitHandle[2];
IAsyncResult oResult1 = oCmd1.BeginExecuteReader();
IAsyncResult oResult2 = oCmd2.BeginExecuteReader();
oHandles[0] = oResult1.AsyncWaitHandle;
oHandles[1] = oResult2.AsyncWaitHandle;
WaitHandle.WaitAll(oHandles);
SqlDataReader oReader1 = oCmd1.EndExecuteReader(oResult1);
SqlDataReader oReader2 = oCmd2.EndExecuteReader(oResult2);
oMyConn.Close();
Note: you cannot use WaitHandles in a Single Threaded Apartment Model (STAThread).

Hope that helps...
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform