Good morning.
Take this simple scenario: I’ve got a NetClient that connects itself to a remote server, using a void method Connect(); when done, NetClient rises Connected event.
I need to wrap this NetClient to another caller that cannot manage async logic like this: this caller wants a sync Connect() method that return true if ok (for simplicity don’t think about connection errors for now).
The code look like this:
public class Client
{
NetClient m_NetClient = new NetClient();
public void Connect(string ip, int port)
{
m_NetClient = new NetClient();
m_NetClient.Connected += _NetClient_Connected;
m_NetClient.Connect(ip, port);
}
private void _NetClient_Connected(object sender, EventArgs e)
{
//...
}
}
What can I do to make this logic sync? I need to rise a new thread in event handler than use something like Monitor class?
Can someone give me his opionin/code for achieve this target?
Thank you all!
A AutoResetEvent should do the trick.