Tuesday, June 25, 2013

IAsyncResult Interface

Represents the status of an asynchronous operation.
The IAsyncResult interface is implemented by classes containing methods that can operate asynchronously. It is the return type of methods that initiate an asynchronous operation, such as FileStream.BeginRead, and it is passed to methods that conclude an asynchronous operation, such as FileStream.EndReadIAsyncResult objects are also passed to methods invoked by AsyncCallback delegates when an asynchronous operation completes.

using System;
using System.Data.SqlClient; 

public class DBProcessing
{
static SqlConnection con = new SqlConnection("initial catalog=cac;data source=WINDOWS-PC\\SQLEXPRESS;integrated security=yes;async=true");
static SqlCommand cmd = new SqlCommand("insert into student values(101,'kaur')");
static SqlCommand cmd1 = new SqlCommand("select * from student");

public static void Perform_TASK_CMD()
{
cmd.Connection = con;
con.Open();
IAsyncResult res=cmd.BeginExecuteNonQuery(null,null); //At this point,New Thread will be allocated for execute the task
Console.WriteLine("Perfect example of Encapsulation...");

while (!res.IsCompleted)
{
Perform_TASK_CMD1(); //This Will run under the Main Thread

}
int t=cmd.EndExecuteNonQuery(res);

}

public static void Perform_TASK_CMD()
{
cmd.Connection = con;
con.Open();
IAsyncResult res=cmd.BeginExecuteNonQuery(null,null); //At this point,New Thread will be allocated for execute the task
Console.WriteLine("Perfect example of Encapsulation...");

while (!res.IsCompleted)
{
Perform_TASK_CMD1(); //This Will run under the Main Thread

}
int t=cmd.EndExecuteNonQuery(res);

}

public static void Perform_TASK_CMD1()
{
Console.WriteLine(" I am Performing");

}

}

class Froent_End
{
// DBProcessing class needs to access frequently

public static void Main() // Single Thread
{

DBProcessing.Perform_TASK_CMD();

}

}

No comments:

Post a Comment