Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manual Reset Events and Asynchronous Programming

Status
Not open for further replies.

Tokhra

Programmer
Oct 1, 2003
134
ES
Hey all,

I've been looking through the microsoft examples of asynchronous socket clients:


When you set waitOne() on a manual reset event, it haults the current thread until the reset event is fired from the aynschronous callback right?

So why bother using asynchronous methods if you are halting threads until they finish anyway? Isn't that the whole point of asynchronous methods, so they don't lock up the application while executing?

Matt.
 
But you can have multiple threads, each waiting for a socket to do something.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
So id put the manual reset event on another thread somehow?
Sorry could you explain that a bit more, its gone straight over my head :/

Thanks very much chiph
Matt.
 
the ManualResetEvent halts the current thread until the Set() method is called on it right?

So:

private ManualResetEvent m_connectDone =
new ManualResetEvent(false);

public void Connect()
{
m_socket.BeginConnect(m_ipEndPoint,
new AsyncCallBack(ConnectCallBack), m_socket);

m_connectDone.WaitOne();
}

private void ConnectCallBack(IAsycResult asyn)
{
m_socket.EndConnect(asyn);

m_connectDone.Set();
}

If in the consumer form I have:

SocketClient client = new SocketClient();
client.Connect();
client.SendData("Hello World");

it won't get to the SendData method until the client is connected right, because the Connect method is waiting for the signal from the callback until it completes?

So if I've got that right, does that mean that while the SocketClient is waiting to get the signal the connection is complete, it will lock up the UI on the form?

If so, how do I get around that? By starting the SocketClient in a seperate thread? Any examples of this?

Thanks,

Matt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top