-
June 8th, 2010, 10:02 AM
#1
newbie question about Event Watchers
I'm trying to learn .net without learning C# first (not my choice) and I just ran across this code:
Code:
watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);
The full source code is below. I don't really get this line, though, and MSDN says nothing about it, does the += mean it's added on a queue (the handler.Arrived event)? I don't need this explained in plain english but if someone could explain that line in C or C++ terms I'd be very grateful, thanks.
sscout
Code:
using System;
using System.Management;
public class EventWatcher
{
public static void Main(string[] args)
{
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent"
, new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
// Initialize an event watcher and subscribe to events
// that match this query
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
MyHandler handler = new MyHandler();
watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);
watcher.Start();
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Process {0} has created, path is: {1}"
, ((ManagementBaseObject)(e.NewEvent["TargetInstance"]))["Name"]
, ((ManagementBaseObject)(e.NewEvent["TargetInstance"]))["ExecutablePath"]);
}
}
}
Last edited by sscout; June 8th, 2010 at 04:07 PM.
Reason: readability
-
June 8th, 2010, 10:56 AM
#2
Re: newbie question about Event Watchers
Firstly - please use code tags to help readability of code.
However in your instance the += means you add the event handler to the event.
So when ever the event in question is triggered, the event handler is called and executed. So when the watcher fires the EventArrived, the EventArrivedEventHandler is called with the handler.Arrived as parameter.
(You can have multiple handlers for an event, thus the += )
I don't know C or C++ so I can't give you the equivalent but plain English must be enough for you to deduct the functionality.
If not - debuging the code as it runs might help you.
-
June 8th, 2010, 11:00 AM
#3
Re: newbie question about Event Watchers
Thanks and sorry, I don't see how to edit my post, or buttons to add , I guess I'll do it manually on my next post.
sscout
-
June 8th, 2010, 03:45 PM
#4
Re: newbie question about Event Watchers
Originally Posted by sscout
Thanks and sorry, I don't see how to edit my post [...]
There should be an edit button in the lower right corner of each of the posts you made (visible only while you're logged in).
Originally Posted by sscout
[...] or buttons to add , I guess I'll do it manually on my next post.
At the bottom of the Quick Reply form, there's a button labeled "Go Advanced". Then you'll be presented with the full-featured UI, that among other things contains a button with the image (#), which inserts the [code][/code] tags.
Anyway, back to your question.
"+=" is an operator that translates to C++ as "+=".
If you're not familiar with it, it's just shorhand for:
x = x + something;
// or
x += something;
In your example, we're talking about events and delegates.
In this case, the += operator effectively adds an event handler to an existing set of other event handlers (which might be empty to begin with).
The += syntax reflects this.
So, the LH operand is basically a group of methods; this way, multiple objects can be notified if needed.
-
June 8th, 2010, 04:09 PM
#5
Re: newbie question about Event Watchers
I don't see the UI, maybe a firefox extension is blocking it...
Anyway, fixed the code tags in 1st post.
-
June 8th, 2010, 04:58 PM
#6
Re: newbie question about Event Watchers
For the UI you must have your preferences set to use the extended WYSIWYG editor. You can turn this on under user CP/ edit options.
Always use [code][/code] tags when posting code.
-
June 8th, 2010, 05:01 PM
#7
Re: newbie question about Event Watchers
Ty, this must be the first forum I'm in where this stuff is off by default.
sscout
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|