15,905,322 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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.