2 questions on Socket and Multithreading

J

John

Hi

*What is the difference between using AsyncCallBack and having a thread
lsitnement data ?

*How can i get the result of a asynchronous call in the same method
that issued the call (by begininvoke)

Thanks
John
 
F

Floyd Burger

John,
AsyncCallback and a separate listening thread basically do the same thing
(use Reflector to see the source for the AsyncCallback mechanism).
The IAsyncResult returned by Begin* has a WaitHandle, you can do a WaitOne
on that to wait until the callback would have happened.
 
J

John

Thanks for you answer.

Could you explain me what is the meaning of the WaitOne or Wait Handle.
I wrote such a program:

public delegate string Call(string p);

class Program
{
static void Main(string[] args)
{
Program myP = new Program();

Call a = new Call(myP.hello);

IAsyncResult ar = a.BeginInvoke("john", null, null);
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}

ar.AsyncWaitHandle.WaitOne();

string response = a.EndInvoke(ar);
Console.WriteLine(response);
Console.ReadLine();

}
public string hello(string p)
{
Thread.Sleep(5000);
return "hello " + p;
}

If i remove or leave the line
ar.AsyncWaitHandle.WaitOne();
i works the same way...

So what is its usefulness...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top