Asynchronous Commands

To enable asynchronous command , the firts step is to set the new Async attribute  to true in the connection string, other wise an exception will be thrown.There can be three different ways to build asynchronous commands : nonblocking , polling,callback

Nonblocking Commands

// Start a non-blocking execution

IAsyncResult iar = cmd.BeginEcecuteReader();

// Do something else meanwhile

........................

// Block the execution untill done

SqlDataReader reader = cmd.EndExecuteReader();

//Process data here ...

Processdata(reader);

Polling

// Executes a query statement

IAsyncResult iar = cmd.BeginEcecuteReader();

do

{

//do something here

}while(iar.IsCompleted);

//Sync

SqlDataReader reader = cmd.EndExecuteReader();

ProcessData(reader);

Callback

// Begin executing the command

IAsyncResult iar = cmd.BeginEcecuteReader( new AsyncCallback(ProcessData),cmd);

After initiating this asynchronous operation , you can forget about it and do other work.The specified callback function is invoked at the end of the operation.

public void ProcessData(IAsyncResult ar)

{

//Retrieve the context of the call

SqlCommand cmd = (SqlCommand) iar.AsyncState;

//Complete the async operation

SqlDataReader reader = cmd.EndExecuteReader();

}

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Saturday, June 17, 2006 2:56 AM

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: