Click here to Skip to main content
15,905,322 members

Comments by Member 2036792 (Top 3 by date)

Member 2036792 2-Apr-13 17:31pm View    
You are right -- if I start my asynchronous handler like this:

static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
String name = (string)e.NewEvent.Properties["ProcessName"].Value;
Console.WriteLine("Process start handler {0}...", name);
Thread.Sleep(10000);
Console.WriteLine("...{0}", name);
...

It always makes it to the second console.writeline().

I'm the one who always kills my handler. That's very helpful! I wonder why my exception handler didn't tell me.

Anyway, this seems to have also solved my not-getting-any-event-at-all problem in the asynchronous example.

There still seems to be two problems.

1. The lingering race condition with resources: I still need to get some info from this event, and look up process info from it. Sometimes the process dies before I can do this. I could try to suspend the process -- but that would be a race condition, too.

2. synchronous example (my other comment post): I still have the not-getting-any-event-at-all problem in the synchronous example -- with quick-exiting processes.
Member 2036792 2-Apr-13 17:06pm View    
Deleted
So, the object I'm looking at does die. You're right.

My goal has been to copy whatever info I need then throw it at another thread. I often don't have enough time to even do that.

So, is there some way to prevent it from dying? I posted the synchronous example to remove the dying event handler from the issue -- what happens is I still have problems with quick-exiting processes. It seems I don't even get events for them.
Member 2036792 2-Apr-13 17:02pm View    
THANK YOU FOR RESPONDING!

so, here's a simple synchronous example (just added a loop to this example: http://msdn.microsoft.com/en-us/library/aa720671(v=vs.71).aspx):

using System;
using System.Management;
using System.Threading;

// This example shows synchronous consumption of events. The client
// is blocked while waiting for events. See additional example for
// asynchronous event handling.

public class EventWatcherPolling
{
public static int 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);

while (true)
{
try
{
Console.WriteLine("Waiting:");
// Block until the next event occurs
// Note: this can be done in a loop if waiting for
// more than one occurrence
ManagementBaseObject e = watcher.WaitForNextEvent();


//Display information from the event
Console.WriteLine(
"IC: {0}",
((ManagementBaseObject)e["TargetInstance"])["Name"]);

}
catch (Exception except)
{
Console.WriteLine("EXCEPTION: " + except.ToString());
}
}

//Cancel the subscription
watcher.Stop();
return 0;
}
}


If you run ipconfig or netstat or stuff that returns quickly, this thing will only pick up some of the events.