0

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);
        }
}
Ken
  • 2,518
  • 2
  • 27
  • 35

1 Answers1

1

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.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Yes you are correct about the object references and (i did not know that about the constructor though) of course my event is not raised there - I was trying to post too quickly and simplified (wrongly so) . You have helped me quite a bit. I found the culprit of my code my event subscription _myProcess.InBoxShareDisconnected += HandleDisconnected(); I had parenthesis declaring it a method and this was really the reason VS was barking about my subscriptions and also not getting the event. I knew it was simple - as I had created my own events and args in other projects. I have it now. Thx.. – Ken Sep 25 '18 at 03:12
  • 1
    Glad you're all straightened out. The only time you would maybe want to raise events in a constructor is if the event were static or in another class. The issue is that you can't subscribe to the event unless the object is constructed, and that doesn't happen until after the constructor is run. You can also use anonymous event handlers, but I tend to shy away from that personally. – Ron Beyer Sep 25 '18 at 03:22