Hi All,
I've already asked another question [wmi newsgroup only], but this is
finally the same issue. I cannot execute two asynchron WMI queries on the
local machine, one after the other. The original question is based on
complicated code. So I reduced the "kernel" to a small C# sample [compilable
to exe], which shows the problem. The question is:
- Is this a problem with my code, as not freeing resources etc?
- Is this possibly a WMI issue?
In the original code, I tried to remove the querying class completely, but
even this does not help. Each second query fails.
Any help would really become very welcomed, each simple tip or hint!!!
Best regards,
Manfred Braun
(Private)
Lange Roetterstrasse 7
D68167 Mannheim
Germany
mailto:_m****** *******@manfbra un.de
(Remove the anti-spam-underscore to mail me!)
Annotation:The executable has to be called with an computername as an
argument. The exe should work with each remote machine, but not with the
local one.
/*
Name: WMIAsyncQuerySa mpleReconnectSa mple5a.cs
*/
using System;
using System.Manageme nt;
class Sample_Manageme ntEventWatcher
{
public static int Main(string[] args)
{
if(args.Length != 0)
{
string comp = args[0];
MyHandler mh;
EventArrivedEve ntHandler eventArrivedEve ntHandler;
ManagementEvent Watcher watcher;
mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived);
watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;
//Start watching for events:
watcher.Start() ;
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
//Stop watching:
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
mh = null;
watcher = null;
eventArrivedEve ntHandler = null;
Console.WriteLi ne("Watcher is stopped, press <enter> to re-start...");
Console.ReadLin e();
mh = new MyHandler();
eventArrivedEve ntHandler = new EventArrivedEve ntHandler(mh.Ar rived);
watcher = Sample_Manageme ntEventWatcher. getWatcher(comp );
watcher.EventAr rived += eventArrivedEve ntHandler;
//Start watching for events:
try
{
watcher.Start() ; //BOMBs here !!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!
Console.WriteLi ne("Watcher is running, press <enter> to stop...");
Console.ReadLin e();
//Stop watching:
watcher.Stop();
watcher.EventAr rived -= eventArrivedEve ntHandler;
}
catch(Exception e)
{
Console.WriteLi ne("Exception:{ 0}", e.ToString());
}
}
else
{
Console.WriteLi ne("Args!!! [P1=Computername]");
}
return 0;
}
public static ManagementEvent Watcher getWatcher(stri ng comp)
{
ConnectionOptio ns co;
ManagementPath mp;
ManagementScope ms;
WqlEventQuery EventQuery;
ManagementEvent Watcher watcher;
string wql;
co = new ConnectionOptio ns();
co.Timeout = new TimeSpan(0, 0, 60);
co.EnablePrivil eges = true;
mp = new ManagementPath( );
mp.NamespacePat h = @"root\cimv2 ";
mp.Server = comp;
ms = new ManagementScope (mp, co);
wql = "select * from __instancecreat ionevent where targetinstance isa
'Win32_NTLogEve nt'";
EventQuery = new WqlEventQuery(w ql);
watcher = new ManagementEvent Watcher(ms, EventQuery);
co = null;
mp = null;
ms = null;
EventQuery = null;
wql = null;
return watcher;
}
public class MyHandler
{
public void Arrived(object sender, EventArrivedEve ntArgs e)
{
ManagementBaseO bject mbo =
(ManagementBase Object)e.NewEve nt["TargetInstance "];
Console.WriteLi ne("Event:{0}" , mbo["message"]);
}
}//Inner class.
}//Class