Hi,
I'm having a problem with event handlers. My code looks (a bit simplified)
like this:
public class Listener
{
CallerClass Call = new CallerClass();
string data= "";
public Listener()
{
Call.OnConnected += new OnConnectedEventHandler(Call_Connected);
Call.OnDataReceived += new
OnDataReceivedEventHandler(Call_DataReceived);
}
private void Call_Connected()
{
//Send confirmation to caller that a connection is established
Call.SendResponse();
//Wait for request
???
//Use request
int dataLength = data.Length;
...
//Send response on the request
Call.SendResponse();
//Wait for new request
???
//Disconnect call
Call.Disconnect();
}
private void Call_DataReceived(string s, int part, int totalparts)
{
//Append
data += s;
//Signal that the whole request is received
if (part == totalparts)
???
}
}
This is my problem:
1. In my class, I have two event handlers. One triggers when a call is
established and one triggers when data is sent in.
2. The first event handler which triggers is Call_Connected. This means that
someone is connected to the device.
3. In the Call_Connected function, I send a confirmation to the caller. This
means that the caller now can send the request. The request is sent in
parts, and receivning the request triggers the Call_DataReceived event.
However, I want to use the request data in the Call_Connected method.
Therefor, I need some sort of mechanism to synchronize these events. For
example, Call_Connected waits until a certain parameter is set by the
Call_DataReceived function and then continues. I have looked at MutEx,
Monitors and AutoResetEvents (looks intersting!?) but can't really find out
if this is the easiest way to do it.
So, does anyone have an idea how I should solve this? Any help i highly
appreciated!
Thanks!
/Mats-Lennart