Click to See Complete Forum and Search --> : Comparing WaitHandle and EndInvoke!!!


mmx_nexus
November 5th, 2007, 06:16 AM
I have a scenario wherin i have to ping two servers inside a method and get back the results.The results is passed to the caller once both the server call returns.For this I'm using an Asyncdelegate/waithandle combination.A sample of the code is mentioned below.



using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WaitHandleConsoleSample
{
public delegate object WorkerDelegate();

class Program
{
static void Main(string[] args)
{
int i = 3;
int x = 7;
WorkerDelegate firstThread = delegate()
{
return Iterator1();
};

WorkerDelegate secondThread = delegate()
{
return Iterator2();
};

IAsyncResult thread1Result = firstThread.BeginInvoke(null, null);

IAsyncResult thread2Result = secondThread.BeginInvoke(null, null);

System.Diagnostics.Debug.WriteLine("WAITING BEGUN IN MAIN THREAD");

WaitHandle.WaitAll(new WaitHandle[] { thread1Result.AsyncWaitHandle, thread2Result.AsyncWaitHandle });

System.Diagnostics.Debug.WriteLine("WAITING ENDED IN MAIN THREAD");

object result1 = firstThread.EndInvoke(thread1Result);


object result2 = secondThread.EndInvoke(thread2Result);

System.Diagnostics.Debug.WriteLine("THREAD 1 Result:" + result1.ToString());

System.Diagnostics.Debug.WriteLine("THREAD 2 Result:" + result2.ToString());

Console.Read();

}

static object Iterator1()
{
int i;
for (i = 0; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine("Thread 1 Value:" + i);
}

return i;
}

static object Iterator2()
{
int i;
for (i = 0; i < 10002; i++)
{
System.Diagnostics.Debug.WriteLine("Thread 2 Value:" + i);
}
return i;
}
}
}




After going through MSDN i found that the same can be achieved using BeginInvoke/EndInvoke combination. i.e EndInvoke waits till the operation returns as in the sample below...



using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WaitHandleConsoleSample
{
public delegate object WorkerDelegate();

class Program
{
static void Main(string[] args)
{
int i = 3;
int x = 7;
WorkerDelegate firstThread = delegate()
{
return Iterator1();
};

WorkerDelegate secondThread = delegate()
{
return Iterator2();
};

IAsyncResult thread1Result = firstThread.BeginInvoke(null, null);

IAsyncResult thread2Result = secondThread.BeginInvoke(null, null);

System.Diagnostics.Debug.WriteLine("WAITING BEGUN IN MAIN THREAD");


object result1 = firstThread.EndInvoke(thread1Result);

System.Diagnostics.Debug.WriteLine("WAITING ENDED IN THREAD 1");

object result2 = secondThread.EndInvoke(thread2Result);

System.Diagnostics.Debug.WriteLine("WAITING ENDED IN THREAD 2");

System.Diagnostics.Debug.WriteLine("THREAD 1 Result:" + result1.ToString());

System.Diagnostics.Debug.WriteLine("THREAD 2 Result:" + result2.ToString());

Console.Read();

}

static object Iterator1()
{
int i;
for (i = 0; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine("Thread 1 Value:" + i);
}

return i;
}

static object Iterator2()
{
int i;
for (i = 0; i < 10002; i++)
{
System.Diagnostics.Debug.WriteLine("Thread 2 Value:" + i);
}
return i;
}
}
}




My question is which one is the best way to wait for an Async operation/operations to return...???Using delegate beginInvoke/WaitHandle/EndInvoke pattern or delegate BeginInvoke/Endinvoke pattern...????

Both yields the same results,both waits until both the operations are completed...So,what really is the difference in both???

Is this the best way to do this kind of an operation,or do you suggest any better ways...???

Thanks in advance for any suggestions,
Mmx

boudino
November 6th, 2007, 04:18 AM
Personally, I would prefere delegate BeginInvoke/Endinvoke pattern, but it is only my subjective opinion, which I cannot prove by exact knowleadge. Maybe because it covers more of implementation details and better supports client's anonymity.