我有一個BackgroundWorker
,它監視要激發的WMI通知事件。我的查詢看起來像SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name=proccessnamehere
。此查詢輪詢每隔2秒滿足WHERE條件的__InstanceDeletionEvent。有時我正在看的過程需要一段時間,延遲事件發生,我想取消它。我讀過的關於取消BackgroundWorker的一切建議我使用某種類型的循環。但是,我不確定如何在通知事件中包含循環。有沒有其他方法可以做到這一點?我打得四處計時器,但沒有成功在等待WMI通知事件時取消BackgroundWorker
編輯:代碼示例 - 刪除無關的代碼
(這將執行鍼對遠程計算機)
using System.Management;
private void backGroundWorkerStart_DoWork(object sender, DoWorkEventArgs e)
{
object[] args = e.Argument as object[];
string computerName = args[0].ToString();
string processName = args[1].ToString();
ConnectionOptions con = new ConnectionOptions();
con.Impersonation = ImpersonationLevel.Impersonate;
con.Authentication = AuthenticationLevel.Default;
con.EnablePrivileges = true;
ManagementScope scope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computerName), con);
scope.Connect();
WqlEventQuery query = new WqlEventQuery(String.Format("SELECT * FROM __InstanceDeletionEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name='{0}'", processName));
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
ManagementBaseObject baseObject = watcher.WaitForNextEvent();
}
你可以發佈一個(可能是簡化版)你當前的代碼? –
@EricJ。添加了一個簡化的示例希望這是你所要求的。 – HiTech