elekgeek
elekgeek

Reputation: 43

Adding events to a class using +=

Please forgive my little knowledge!

I have the following class in HIDNewDeviceEventMonitor.cs:

public class HIDNewDeviceEventMonitor : IDisposable
{
    // used for monitoring plugging and unplugging of USB devices.
    private ManagementEventWatcher watcherAttach;

    public HIDNewDeviceEventMonitor()
    {
        // Catch USB HID plugged instance event watching
        watcherAttach = new ManagementEventWatcher();
        watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
        watcherAttach.Query = new WqlEventQuery(@"SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PNPEntity' AND TargetInstance.DeviceID LIKE 'HID\\VID_04D8%'");
        watcherAttach.Start();
    }

    void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Debug.WriteLine("my device is inserted..");
    }

    public void Dispose()
    {
        watcherAttach.Stop();
        watcherAttach.Dispose();
    }

    ~HIDNewDeviceEventMonitor()
    {
        this.Dispose();
    }
}

Now, how can I change this class to be able to add an event handler that the class can call from within watcher_EventArrived where someNewEvent is outside the class file, actually in the form.cs:

// code in the form
HIDNewDeviceEventMonitor ok = new HIDNewDeviceEventMonitor();
ok.Inserted += someNewEvent;  // <-- my problem, I don't know how to add an event to the class this way

private void someNewEvent()
{
    //Enumerate and add to listbox1
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    ok.Dispose();
}

I 've seen this thing with other classes, how can I make my class like that?

Upvotes: 0

Views: 346

Answers (3)

havish
havish

Reputation: 373

You need to do following in the HIDNewDeviceEventMonitor class:

1.) First define a public event inside the class like this-

public event EventHandler Inserted; 2.) Then fire this event within the code where you detect the changes in events. Like this-

if(Inserted != null) Inserted(this,null);

The if condition checks if the event is registered by any listener. It's fired in case it is. Hope this helps.

Upvotes: 0

Cameron
Cameron

Reputation: 2594

Simply put, you're trying to add events to your HIDNewDeviceMonitor class.

To do this, first you'll need to define a delegate.

public delegate void InsertedHandler;

Next, you'll need to define the event in your HIDNewDeviceMonitor class.

// Notice how the event uses the delegate that's been defined
//               v       
public event InsertedHandler Inserted;

Now you'll need something that "fires" the event, which could easily be put in your watcher_EventArrived method.

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
    Debug.WriteLine("my device is inserted..");

    // Notice how we check the event handler for null.
    // If you don't, it could throw a NullReferenceException.
    // Irritating, but easy to debug.. Usually..
    if (Inserted != null)
        Inserted(); // Or whatever parameters you need.
}

We're all done with the HIDNewDeviceMonitor class.

Now whatever class that uses the HIDNewDeviceMonitor can use the EventHandler code that you provided.

However, it'll have to be the same delegate.

public class MyClass
{
  HIDNewDeviceMonitor monitor;
  public MyClass()
  {
    monitor = new HIDNewDeviceMonitor();
    monitor.Inserted += DeviceInserted;
  } 

  private void DeviceInserted() 
  { 
   // Execute code here
  }
}

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 157098

Your Inserted event should look like this:

public event EventHandler Inserted;

You invoke it like this:

private void OnInserted()
{
    if (this.Inserted != null)
    {
        this.Inserted(this, EventArgs.Empty);
    }
}

The signature for the event handler is this:

void someNewEvent(object sender, EventArgs e)
{
    //
}

Then you should wrap that code in the constructor of the class:

HIDNewDeviceEventMonitor ok;

public ClassName()
{
   ok = new HIDNewDeviceEventMonitor();
   ok.Inserted += someNewEvent;  // <-- my problem
}

Declare the ok variable outside the constructor, and instantiate it inside. Then add the event handler.

Pro tip: You could use the generic EventHandler<T> if you need to supply a custom implementation of e.

Upvotes: 2

Related Questions