Wmi EventWatcher 缺少事件 - 修复?
Wmi EventWatcher missing events - fix?
我正在尝试监控我们的服务器,并且需要捕获 __InstanceCreationEvent
以便在创建 Win32_PrintJob
时收到通知。为此,我尝试使用 ManagementEventWatcher
,效果几乎不错,但 Watcher 错过了一些 PrintJob 创作。
ManagementEventWatcher watcher =
new ManagementEventWatcher(Server.Name + @"\root\cimv2",
"Select * From __InstanceCreationEvent Within 1
Where TargetInstance ISA 'Win32_PrintJob'");
watcher.EventArrived += PrintJobCreated;
watcher.Start();
每当创建非常 short/little 的 PrintJob 时,例如一个测试页,我不会一直收到创建事件。我不知道,但我想这是因为 PrintJob 是在 1 秒计时器用完之前完成的,这可能吗?
不过。有没有办法获得所有 PrintJob
创作的 100%?
或者使用另一种机制比 WMI-watcher 更好吗?
更新
我试过将 within
更改为 0.1
效果更好,但是 100 毫秒在这里有意义吗?
WMI 中的所有内部事件(如 __InstanceCreationEvent)都使用轮询来检测更改 - 总是有可能错过特别短的事件。通过减少轮询间隔,您肯定会提高检测率,但绝对不能保证。
另请记住,WMI 轮询查询会消耗大量 CPU 资源,您不会提及您的环境,但一般来说,您要监视的打印队列越多,CPU 利用率就越高你会看到的。如果增加轮询率,效果将会放大。这反过来可能会导致错过更多活动等。
如果您绝对需要准确性,您可以查看 FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification Win32 API functions however it's a bit more complex to implement than your WMI solution. There's some guidance at Code Project 如何使其在 .NET/C# 中发挥作用。
我正在尝试监控我们的服务器,并且需要捕获 __InstanceCreationEvent
以便在创建 Win32_PrintJob
时收到通知。为此,我尝试使用 ManagementEventWatcher
,效果几乎不错,但 Watcher 错过了一些 PrintJob 创作。
ManagementEventWatcher watcher =
new ManagementEventWatcher(Server.Name + @"\root\cimv2",
"Select * From __InstanceCreationEvent Within 1
Where TargetInstance ISA 'Win32_PrintJob'");
watcher.EventArrived += PrintJobCreated;
watcher.Start();
每当创建非常 short/little 的 PrintJob 时,例如一个测试页,我不会一直收到创建事件。我不知道,但我想这是因为 PrintJob 是在 1 秒计时器用完之前完成的,这可能吗?
不过。有没有办法获得所有 PrintJob
创作的 100%?
或者使用另一种机制比 WMI-watcher 更好吗?
更新
我试过将 within
更改为 0.1
效果更好,但是 100 毫秒在这里有意义吗?
WMI 中的所有内部事件(如 __InstanceCreationEvent)都使用轮询来检测更改 - 总是有可能错过特别短的事件。通过减少轮询间隔,您肯定会提高检测率,但绝对不能保证。
另请记住,WMI 轮询查询会消耗大量 CPU 资源,您不会提及您的环境,但一般来说,您要监视的打印队列越多,CPU 利用率就越高你会看到的。如果增加轮询率,效果将会放大。这反过来可能会导致错过更多活动等。
如果您绝对需要准确性,您可以查看 FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification Win32 API functions however it's a bit more complex to implement than your WMI solution. There's some guidance at Code Project 如何使其在 .NET/C# 中发挥作用。