newbie question about Event Watchers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: newbie question about Event Watchers

  1. #1
    Join Date
    Jun 2010
    Location
    .net 2.0 3.0 3.5 4.0 VS 2005, VSE 2010
    Posts
    4

    Question 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 05:07 PM. Reason: readability

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    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.

  3. #3
    Join Date
    Jun 2010
    Location
    .net 2.0 3.0 3.5 4.0 VS 2005, VSE 2010
    Posts
    4

    Resolved Re: newbie question about Event Watchers

    Thanks and sorry, I don't see how to edit my post, or buttons to add
    Code:
     tags
    , I guess I'll do it manually on my next post.

    sscout

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

    Re: newbie question about Event Watchers

    Quote Originally Posted by sscout View Post
    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).

    Quote Originally Posted by sscout View Post
    [...] or buttons to add
    Code:
     tags
    , 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.

  5. #5
    Join Date
    Jun 2010
    Location
    .net 2.0 3.0 3.5 4.0 VS 2005, VSE 2010
    Posts
    4

    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.

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,851

    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.

  7. #7
    Join Date
    Jun 2010
    Location
    .net 2.0 3.0 3.5 4.0 VS 2005, VSE 2010
    Posts
    4

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center