Asynchronous execution of the commands in ADO.Net 2.0 :-
Difference Between Synchronous Call and Asynchronous Call A synchronous call blocks a process until the operation completes. Once each command is completed the process executes the next instruction. While in asynchronous calls, the process need not to wait for the completion of one command for the other when they are independent.
The asynchronous command execution feature in ADO.NET 2.0 is has improved data access efficiency and reduced latency and delays in the Web.
There are three design approaches for asynchronous model:
1. Polling :
Step 1) Start asynchronous command execution
IAsyncResult result = MyCommand.BeginExecuteReader()
Step 2) Wait until execution is complete:
while (! result.IsCompleted) { // execute other code here }
Step 3) Fetch results
SqlDataReader reader = MyCommand.EndExecuteReader(result )
2. Wait :
Step1) Start one or more asynchronous commands as an array of IAsyncResult instances:
IAsyncResult resultx = MyCommand.BeginExecuteReader()
Step2) Wait for each command to complete
for(int i=0; i < result_array.Length, i++) { index = WaitHandle.WaitAny(result_array, timeout, true); switch(index) { case 0: SqlDataReader reader = MyCommand.EndExecuteReader(resultx); // ...etc...
3. Callback :
Step1) Start execution, specifying callback and passing command as the AsyncState:
MyCommand.BeginExecuteReader(new AsyncCallback(MyCallback), cmd)
Step2) Provide a callback handler :
void MyCallback(IAsyncResult result) { SqlCommand cmd = (SqlCommand) result.AsyncState; SqlDataReader reader = cmd.EndExecuteReader(result); }
By using any of the three models, you can achieve the asynchronous calling in ADO.Net.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|