/***********************************************************************
Synchronous(blocking) calls vs Asynchronous(non-blocking) calls with C#
***********************************************************************/
namespace Test
{
class Program
{
static void Add(int a, int b)
{
Console.WriteLine(a.ToString() + " + " + b.ToString() + " = " + (a + b).ToString());
}
static void Mul(int a, int b)
{
Console.WriteLine(a.ToString() + " X " + b.ToString() + " = " + (a * b).ToString());
}
static void Process()
{
int i;
for (i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(i.ToString() + " ");
}
}
static void Main(string[] args)
{
Add(3, 4);
Process(); // assume that this will take long time to process some data
Mul(2, 3); // this guy has to wait for Process() to complete though there is no dependency
}
}
}
/***********************************************************************
To avoid the waiting situation for Mul(), just change the Main() as below
***********************************************************************/
delegate void ProcessDelegate();
static void Main(string[] args)
{
ProcessDelegate dlg = new ProcessDelegate(Process);
Add(3, 4);
IAsyncResult ar = dlg.BeginInvoke(null,null); // no callback after invoking
Mul(2, 3); // no waiting for Process() to complete
dlg.EndInvoke(ar);
}