EDN Admin
Well-known member
Ive been battling what I thought would be a small problem when I started it, to what has now caused me to a day or so of puzzlement. I am trying to instantiate a ManagementEventWatcher to monitor the clock on my pc and simply display the time. The problem
I am running into is that as soon as the watcher starts, it throws and invalid class that is being thrown as a management exception.
I followed a few threads here on the forums, and thought it might be that I need to specify an eventclassname for the watcher, but I thought I would post the code here, so that perhaps someone might see the problem I am creating for myself.
As I have not worked much with WMI, any help is much appreciated!
Platform: Windows 7
Visual Studio 2010
---------- code begins------------------------
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Verdana; color:#000000 /// <summary><br/>
/// Interaction logic for MainWindow.xaml<br/>
/// </summary><br/>
public partial class MainWindow : Window<br/>
{<br/>
ManagementEventWatcher watcher = new ManagementEventWatcher();
<span style="font-family:Verdana; color:#000000 public MainWindow()<br/>
{<br/>
InitializeComponent();<br/>
}
<span style="font-family:Verdana; color:#000000 private void Window_Loaded(object sender, RoutedEventArgs e)<br/>
{<br/>
MessageBox.Show("The App started..");<br/>
//eventreceiver defined in this namespace<br/>
EventReceiver receiver = new EventReceiver();
<span style="font-family:Verdana; color:#000000 //create watcher and register the callback<br/>
watcher = GetWatcher(new EventArrivedEventHandler(receiver.OnEventArrived));<br/>
//watcher starts to listen to management events<br/>
try<br/>
{<br/>
watcher.Start();<br/>
}<br/>
catch (AccessViolationException ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " access exception");<br/>
}<br/>
catch (ManagementException ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " management exception");<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " general exception");<br/>
} <br/>
}
<span style="font-family:Verdana; color:#000000 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)<br/>
{<br/>
watcher.Stop();<br/>
}
<span style="font-family:Verdana; color:#000000 public static ManagementEventWatcher GetWatcher(EventArrivedEventHandler handler)<br/>
{ <br/>
//create event query to be notified within 1 second of a change in a service<br/>
WqlEventQuery query = new WqlEventQuery("_InstanceModificationEvent", new TimeSpan(0, 0, 1),<br/>
"TargetInstance isa Win32_Localtime AND " + "TargetInstance.Second = 0");<br/>
ManagementScope scope = new ManagementScope(@" file://\.rootdefault \.rootdefault ");<br/>
//initialize an event watcher and subscribe to events that match this query<br/>
ManagementEventWatcher watcher = new ManagementEventWatcher(scope,query);<br/>
//attach the event arrived property to eventarrivedeventhandler method with the
<br/>
//required handler to allow watcher to communicate to the app<br/>
watcher.EventArrived += new EventArrivedEventHandler(handler);<br/>
return watcher;<br/>
}<br/>
}
<span style="font-family:Verdana; color:#000000 public class EventReceiver<br/>
{<br/>
public void OnEventArrived(object sender, EventArrivedEventArgs e)<br/>
{<br/>
ManagementBaseObject evt = e.NewEvent;
<span style="font-family:Verdana; color:#000000 //display information for the event<br/>
string time = string.Format("{0}:{1:00}",<br/>
((ManagementBaseObject)evt["TargetInstance"])["Hour"],<br/>
((ManagementBaseObject)evt["TargetInstance"])["Minute"]);
<span style="font-family:Verdana; color:#000000 MessageBox.Show(time, "Current time");<br/>
}<br/>
}
<span style="font-family:Verdana; color:#000000 ------- code ends -------
View the full article
I am running into is that as soon as the watcher starts, it throws and invalid class that is being thrown as a management exception.
I followed a few threads here on the forums, and thought it might be that I need to specify an eventclassname for the watcher, but I thought I would post the code here, so that perhaps someone might see the problem I am creating for myself.
As I have not worked much with WMI, any help is much appreciated!
Platform: Windows 7
Visual Studio 2010
---------- code begins------------------------
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Verdana; color:#000000 /// <summary><br/>
/// Interaction logic for MainWindow.xaml<br/>
/// </summary><br/>
public partial class MainWindow : Window<br/>
{<br/>
ManagementEventWatcher watcher = new ManagementEventWatcher();
<span style="font-family:Verdana; color:#000000 public MainWindow()<br/>
{<br/>
InitializeComponent();<br/>
}
<span style="font-family:Verdana; color:#000000 private void Window_Loaded(object sender, RoutedEventArgs e)<br/>
{<br/>
MessageBox.Show("The App started..");<br/>
//eventreceiver defined in this namespace<br/>
EventReceiver receiver = new EventReceiver();
<span style="font-family:Verdana; color:#000000 //create watcher and register the callback<br/>
watcher = GetWatcher(new EventArrivedEventHandler(receiver.OnEventArrived));<br/>
//watcher starts to listen to management events<br/>
try<br/>
{<br/>
watcher.Start();<br/>
}<br/>
catch (AccessViolationException ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " access exception");<br/>
}<br/>
catch (ManagementException ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " management exception");<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
MessageBox.Show(ex.Message + " general exception");<br/>
} <br/>
}
<span style="font-family:Verdana; color:#000000 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)<br/>
{<br/>
watcher.Stop();<br/>
}
<span style="font-family:Verdana; color:#000000 public static ManagementEventWatcher GetWatcher(EventArrivedEventHandler handler)<br/>
{ <br/>
//create event query to be notified within 1 second of a change in a service<br/>
WqlEventQuery query = new WqlEventQuery("_InstanceModificationEvent", new TimeSpan(0, 0, 1),<br/>
"TargetInstance isa Win32_Localtime AND " + "TargetInstance.Second = 0");<br/>
ManagementScope scope = new ManagementScope(@" file://\.rootdefault \.rootdefault ");<br/>
//initialize an event watcher and subscribe to events that match this query<br/>
ManagementEventWatcher watcher = new ManagementEventWatcher(scope,query);<br/>
//attach the event arrived property to eventarrivedeventhandler method with the
<br/>
//required handler to allow watcher to communicate to the app<br/>
watcher.EventArrived += new EventArrivedEventHandler(handler);<br/>
return watcher;<br/>
}<br/>
}
<span style="font-family:Verdana; color:#000000 public class EventReceiver<br/>
{<br/>
public void OnEventArrived(object sender, EventArrivedEventArgs e)<br/>
{<br/>
ManagementBaseObject evt = e.NewEvent;
<span style="font-family:Verdana; color:#000000 //display information for the event<br/>
string time = string.Format("{0}:{1:00}",<br/>
((ManagementBaseObject)evt["TargetInstance"])["Hour"],<br/>
((ManagementBaseObject)evt["TargetInstance"])["Minute"]);
<span style="font-family:Verdana; color:#000000 MessageBox.Show(time, "Current time");<br/>
}<br/>
}
<span style="font-family:Verdana; color:#000000 ------- code ends -------
View the full article