I have a class that calls another class - the new class has events that I have defined for it. I am subscribed to the events in my calling class but my calling class does not seem to be able to get the EventArgs. I know I must be doing something ignorant here but I don't know what.
My code abbreviated below. WorkControl is the main process and calls MyProcess which executes some code and fires off the event.
public class WorkControl
{
public MyProcess myp;
public WorkControl()
{
myp.InBoxShareDisconnected += OnShareFolderDisconnected();
}
private EventHandler OnShareFolderDisconnected<NetworkShareDisconnectedEventArgs>()
{
// How do I get my EventArgs from the event?
throw new NotImplementedException();
}
}
public class MyProcess
{
public void MyDisconnectTrigger
{
NetworkShareDisconnectedEventArgs e =
new NetworkShareDisconnectedEventArgs(path, timestamp, connected);
OnInBoxShareDisconnected(e);
}
public event EventHandler<NetworkShareDisconnectedEventArgs> InBoxShareDisconnected;
protected void OnInBoxShareDisconnected(NetworkShareDisconnectedEventArgs e)
{
// InBoxShareDisconnected(this, e);
InBoxShareDisconnected.SafeInvoke(this, e);
}
}
You have a couple problems. Your
MyProcess
class shouldn't raise events in the constructor and theMyWorker
class needs to have an instance ofMyProcess
to attach the event to. The other problem is that you need to declare the event handler correctly.Lets look at the proper event pattern for your producer
MyProcess
class:And now we can look at your consumer class:
So now you can consume the events in the consumer class and have access to all the properties of the
NetworkShareDisconnectedEventArgs
arguments. This is a pretty standard event producer/consumer model.