Click here to Skip to main content
15,897,704 members

Process Information and Notifications using WMI

Andy Lanng asked:

Open original thread
Hi,

I posted this question on an article just now but I don't know how many people would see it there:
Process Information and Notifications using WMI[^]

So anyway. Many of the concepts I am using are explained in the article but here is my problem:

It just so happens that I have implemented almost identical code in a testing process monitor I have written. I use the code to keep an eye on any child (and [great]*grand kids) of a test process I have started. I do this because I need to know when the entire process completes and so I can kill any child processes if the test times out.

This code works fine 99.9% of the time but I am getting some rare errors specifically on Windows 7 computers and servers.
testing the code on my own windows 7 machine often gives me most common error which is "Exception from HRESULT: 0xFFFFFFFF" but LastWin32Error is 1008:
0x000003F0
ERROR_NO_TOKEN
An attempt was made to reference a token that does not exist.

Here is the code that triggers the error:
C#
    const string queryString = "SELECT *" +
                               "  FROM __InstanceOperationEvent " +
                               "WITHIN  " + pol +
                               " WHERE TargetInstance ISA 'Win32_Process' ";

    MachineName = ".";
    // You could replace the dot by a machine name to watch to that machine
    const string scope = @"\\.\root\CIMV2";

    // create the watcher and start to listen

    _watcher = new ManagementEventWatcher(scope, queryString);
_watcher.EventArrived += WatcherOnEventArrived;
    _watcher.Start(); // ERROR
    _watcher.WaitForNextEvent();


Is would appear that once this error occurs once then it is almost certain to happen the next several times I try, so try-catch-retry code doesn't help.

Any ideas what I can do to get by this or look into it further?

Thanks
A
Tags: C#, Windows, Windows 7, Visual Studio, WMI

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900