FindNextPrinterChangeNotification是否会错过事件?

时间:2016-01-12 10:16:07

标签: c# .net events unmanaged print-spooler-api

我正在使用FindFirstPrinterChangeNotificationFindNextPrinterChangeNotification来捕捉打印事件。但是我注意到FindNextPrinterChangeNotification无法可靠地返回所有事件。我找到了一个有同样问题的家伙in this article

基本上,当我调试程序时,或者在处理事件时将Sleep命令与他的建议放在一起时,FindNextPrinterChangeNotification会跳过很多事件。此外,大多数时候我收到很多SPOOLING状态事件但是错过了DELETED状态事件(有时我得到它,但大部分时间我都不能),即使我已经将作业推送到队列以供以后处理。 / p>

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

编辑经过一番调查后,事件实际上并没有消失。如果我调用另一个打印作业,则会触发先前的事件(包括上一个打印作业的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);

1 个答案:

答案 0 :(得分:1)

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

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

使用:

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

(最后的假arg) 并且似乎现在正在工作