Getting EventArgs in a calling class

281 Views Asked by At

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);
        }
}
1

There are 1 best solutions below

2
On BEST ANSWER

You have a couple problems. Your MyProcess class shouldn't raise events in the constructor and the MyWorker class needs to have an instance of MyProcess 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:

public class MyProcess
{
    public event EventHandler<NetworkShareDisconnectedEventArgs> InBoxShareDisconnected;

    public MyProcess()
    {
        //This doesn't really do anything, don't raise events here, nothing will be
        //subscribed yet, so nothing will get it.
    }

    //Guessing at the argument types here
    public void Disconnect(object path, DateTime timestamp, bool connected)
    {
        RaiseEvent(new NetworkShareDisconnectedEventArgs(path, timestamp, connected));
    }

    protected void RaiseEvent(NetworkShareDisconnectedEventArgs e)
    {
        InBoxShareDisconnected?.Invoke(this, e);
    }
}

And now we can look at your consumer class:

public class WorkControl
{
    private MyProcess _myProcess;

    public WorkControl(MyProcess myProcess)
    {
        _myProcess = myProcess;  //Need to actually set it to an object
        _myProcess.InBoxShareDisconnected += HandleDisconnected;
    }

    private void HandleDisconnected(object sender, NetworkShareDisconnectedEventArgs e)
    {
        //Here you can access all the properties of "e"
    }
}

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.