在我的应用程序中,我想在笔式驱动器附加和分离事件上引发事件。我使用以下代码来引发事件。
private ManagementEventWatcher watcherAttach;
private ManagementEventWatcher watcherRemove;
watcherAttach = new ManagementEventWatcher();
watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcherAttach.Start();
watcherRemove = new ManagementEventWatcher();
watcherRemove.EventArrived += new EventArrivedEventHandler(watcher_EventRemoved);
watcherRemove.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
watcherRemove.Start();
// this are events
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
// code to execute
}
void watcher_EventRemoved(object sender, EventArrivedEventArgs e)
{
//code to execute
}
但问题是这两个事件都引发了两次,并且我的代码在这两个事件中都没有完全执行,有人可以建议我如何克服这个问题吗?