本文整理汇总了C#中System.Management.ManagementEventWatcher.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ManagementEventWatcher.Dispose方法的具体用法?C# ManagementEventWatcher.Dispose怎么用?C# ManagementEventWatcher.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.ManagementEventWatcher
的用法示例。
在下文中一共展示了ManagementEventWatcher.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: USBRemoveWacher
/// <summary>
/// USB 插入和拔出监测函数。
/// 使用ManagementEventWacher来预定特定系统事件,通过WqlEventQuery设置查询对象和条件以及其他属性(比如查询的轮询间隔),
/// 通过ManagementScope设置查询路径范围。
/// </summary>
public void USBRemoveWacher()
{
ManagementEventWatcher wacher = null;
WqlEventQuery query = null;
ManagementScope scope = null;
try
{
scope = new ManagementScope("root\\CIMV2"); //设置WMI路径
query = new WqlEventQuery(); //设置查询的事件类名,条件,查询间隔,也可一次在构造函数中初始化
query.EventClassName = "__InstanceDeletionEvent";
query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
query.WithinInterval = new TimeSpan(1000);
wacher = new ManagementEventWatcher(scope, query);
wacher.EventArrived += new EventArrivedEventHandler(onUSBRemoved);
wacher.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Closed += (s, e) =>
{
wacher.Stop();
wacher.Dispose();
};
}
示例2: WaitForAsync
public Task WaitForAsync(string name, CancellationToken cancellationToken)
{
_logger.WriteLine("Attempting {0} wait.", IsEfficient ? "efficient" : "inefficient");
var taskSource = new TaskCompletionSource<object>();
var query = IsEfficient
? new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName = '" + name + "'")
: new EventQuery("SELECT TargetInstance FROM __InstanceCreationEvent WITHIN 0.5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name LIKE '" + name + "'");
ManagementEventWatcher watcher = null;
try {
watcher = new ManagementEventWatcher(query);
cancellationToken.Register(() => {
_logger.WriteLine("Received Cancel while waiting for '{0}'.", name);
watcher.Stop();
watcher.Dispose();
taskSource.TrySetCanceled();
});
watcher.EventArrived += (sender, e) => {
_logger.WriteLine("Received process start event for '{0}'.", name);
watcher.Stop();
watcher.Dispose();
taskSource.TrySetResult(null);
};
try {
watcher.Start();
}
catch (ManagementException) {
if (!IsEfficient)
throw;
watcher.Dispose();
IsEfficient = false;
return WaitForAsync(name, cancellationToken);
}
}
catch (Exception) {
if (watcher != null)
watcher.Dispose();
throw;
}
return taskSource.Task;
}
示例3: Run
//.........这里部分代码省略.........
processId[0] = createProcess(scope, arguments);
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query =
new WqlEventQuery("SELECT * From WIN32_ProcessStopTrace WHERE ProcessID=" + getProcessId());
// Initialize an event watcher and subscribe to events
// that match this query
watcher =
new ManagementEventWatcher(scope, query);
// times out watcher.WaitForNextEvent in 5 seconds
watcher.Options.Timeout = new TimeSpan(0, 0, (int)wait);
// Block until the next event occurs
// Note: this can be done in a loop if waiting for
// more than one occurrence
ManagementBaseObject e = watcher.WaitForNextEvent();
//Cancel the subscription
watcher.Stop();
return exitCode;
/*
* // TODO: THe following can be implemented for detecting timeouts & restarted servers to prevent issues with timeout exceptions
* http://stackoverflow.com/questions/12159989/asynchronous-wmi-event-handling-in-net
ManagementEventWatcher w = new ManagementEventWatcher(managementScope, new WqlEventQuery("select * from Win32_ProcessStopTrace where ProcessId=" + ProcessId));
w.Options.Timeout = new TimeSpan(0, 0, 0, 0, 1);
DateTime start = DateTime.Now;
while (Status == StatusNotStarted) //default status(just strings)
{
try
{
var ev = w.WaitForNextEvent();
ReturnCode = (uint)ev.Properties["ExitStatus"].Value;
Status = (ReturnCode == 0) ? StatusOk : StatusFailed;
}
catch (ManagementException ex)
{
if (!ex.Message.Contains("Timed out"))
throw ex;
try
{
Ping p = new Ping();
PingReply reply = p.Send(MachineIP);
if (reply.Status != IPStatus.Success)
Status = StatusFailed;
else
{
DateTime end = DateTime.Now;
TimeSpan duration = end - start;
if (duration.TotalMilliseconds > Timeout)
{
Status = StatusFailed;
}
}
}
*/
}
catch (Exception ex)
{
var msg = dumpRunArguments(command, wait);
msg = string.Format("Command execution failed:\n{0}", msg);
if (Log.WriteLog != null)
{
Log.WriteLog("ERROR: " + msg);
}
if (ex is TimeoutException)
{
if (Log.WriteLog != null)
{
Log.WriteLog("ERROR: Exception is timeout, trying to kill process: "+ ex.Message);
}
if ((scope != null) && (processId[0] != 0))
{
bool found;
tryKillProcess(scope, processId[0], out found);
}
}
throw new Exception(msg, ex);
}
finally
{
if (watcher != null)
{
watcher.Stop();
watcher.Dispose();
}
}
}