FindNextPrinterChangeNotification 错过事件?

FindNextPrinterChangeNotification misses events?

我正在使用 FindFirstPrinterChangeNotificationFindNextPrinterChangeNotification 来捕获打印事件。但是我注意到 FindNextPrinterChangeNotification 并不能可靠地 returns 所有事件。我找到了一个有同样问题的人 in this article.

基本上,当我调试我的程序时,或者在处理事件时按照他的建议放置睡眠命令时,FindNextPrinterChangeNotification会跳过很多事件。此外,大多数时候我会收到很多 SPOOLING 状态事件,但会错过 DELETED 状态事件(有时我会收到,但大多数时候我不能收到),即使我已经将作业推送到队列以供以后处理。

有没有人也有这个问题?另外,我正在尝试 Microsoft PDF 打印机,NumberOfPages 随着 SPOOLING 事件的到来而增加,但 NumberOfPagesPrinted 没有。是故意的吗?

EDIT 经过一番调查,事件实际上并没有消失。如果我调用另一个打印作业,则会触发之前的事件(包括之前打印作业的 DELETING/DELETED 状态)。你能指出问题是什么吗?

调用FindFirstPrinterChangeNotification的代码如下:

    //We got a valid Printer handle.  Let us register for change notification....
    _changeHandle = FindFirstPrinterChangeNotification(_printerHandle, (int)PRINTER_CHANGES.PRINTER_CHANGE_JOB, 0, _notifyOptions);
    // We have successfully registered for change notification.  Let us capture the handle...
    _mrEvent.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(_changeHandle, true);
    //Now, let us wait for change notification from the printer queue....
    _waitHandle = ThreadPool.RegisterWaitForSingleObject(_mrEvent, new WaitOrTimerCallback(PrinterNotifyWaitCallback), _mrEvent, -1, true);

这是 FindNextPrinterChangeNotification:

    _notifyOptions.Count = 1;
    _notifyOptions.dwFlags = PRINTER_NOTIFY_OPTIONS_REFRESH;
    int pdwChange = 0;
    IntPtr pNotifyInfo = IntPtr.Zero;
    bool bResult = FindNextPrinterChangeNotification(_changeHandle, out pdwChange, _notifyOptions, out pNotifyInfo);

我遇到了同样的问题然后我尝试了:

_waitHandle = ThreadPool.RegisterWaitForSingleObject(_mrEvent, new WaitOrTimerCallback(PrinterNotifyWaitCallback), _mrEvent, -1, true);    

与:

_waitHandle = ThreadPool.RegisterWaitForSingleObject(_mrEvent, new WaitOrTimerCallback(PrinterNotifyWaitCallback), _mrEvent, -1, false);

(最后是 false arg) 现在似乎可以工作了