Yes, .Net provides many asyn methods in .net from 1.0 version.
In case of synchronus programming:
Client has to wait/block till present thread is process to execute the remaining lines of code.
Instead of waiting much time .net provides few methods for creating thread for asynchronus programming.
In Ado.net
public IAsyncResult BeginExecuteNonQuery();
public IAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject);
public IAsyncResult BeginExecuteReader();
public IAsyncResult BeginExecuteReader(CommandBehavior behavior);
public IAsyncResult BeginExecuteReader();
public IAsyncResult BeginExecuteReader(CommandBehavior behavior);
.......………….ect
BeginExecuteNonQuery()
is a asynchronous method which is used to execute a command in new thread.
advantage is the remaining lines of main method code executed through main thread.
IAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject);
Here
AsyncCallback: is a delegate which reference a method when corresponding asynchronous operation completes.
stateObject:
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the System.IAsyncResult.AsyncState property.
Demo:
BeginExecuteNonQuery()
is a asynchronous method which is used to execute a command in new thread.
advantage is the remaining lines of main method code executed through main thread.
which returns IAsyncResult which is passed to EndExecuteNonQuery() for result(of sqlcommand--command or stored procedure).
as well it has one more overloaded methodIAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject);
Here
AsyncCallback: is a delegate which reference a method when corresponding asynchronous operation completes.
stateObject:
A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback procedure using the System.IAsyncResult.AsyncState property.
Demo:
class Program
{
static void Main(string[] args)
{
string sqlConnectString ="uid=sa;pwd=zolt123$;database=wcfdb;Asynchronous Processing=true";
Console.WriteLine("(Press any key to exit.)\n");
SqlConnection connection = new SqlConnection(sqlConnectString);
string sqlstring = "update emp set eloc='Hyd' where eloc='Hyds'";
SqlCommand command = new SqlCommand(sqlstring, connection);
Console.WriteLine("Main Thread is :" +Thread.CurrentThread.ManagedThreadId.ToString());
connection.Open();
// Start the async operation.
//The HandleCallback() method is called when the operation completes in 5 seconds.
command.BeginExecuteNonQuery(new AsyncCallback(HandleCallback),command);
Console.WriteLine("[{0}] BeginExecuteNonQuery() called."+ Thread.CurrentThread.ManagedThreadId.ToString(),
DateTime.Now);
Console.WriteLine("Data update in progress. Please wait!");
for (int i = 0; i < 2; i++)
{
Console.Write(i);
Console.WriteLine("Main Thread is : " + Thread.CurrentThread.ManagedThreadId.ToString());
}
Console.ReadKey();
}
private static void HandleCallback(IAsyncResult asyncResult) //***
{
// get the original object
SqlCommand command = (SqlCommand)asyncResult.AsyncState;
// get and display the rows affected
int rowsAffected = command.EndExecuteNonQuery(asyncResult);
Console.WriteLine("[{0}] EndExecuteNonQuery() called.",
DateTime.Now);
Console.WriteLine("\nRows affected = {0}", rowsAffected);
Console.WriteLine("Sub Thread is :"+ Thread.CurrentThread.ManagedThreadId.ToString());
// close the connection
command.Connection.Close();
}
}
//*** we need to pass the reference of the current object state to callback method
//*** we need to pass the reference of the current object state to callback method
output
NOTE:
NOTE:
1. "Asynchronous Processing=true";
2. if we don't want to pass any callback we can set as null
Ex:
IAsyncResult BeginExecuteNonQuery(AsyncCallback callback, object stateObject);
like
IAsyncResult BeginExecuteNonQuery(null,null);
this IAsyncResult is used in wcf to asynchronously call
the service.
No comments:
Post a Comment