2014-10-19 64 views
1

我想在打印作業完成或刪除時收到通知。現在我看到通知機制提供了JOB_STATUS_DELETING,但不能獲得JOB_STATUS_DELETED狀態。無法獲取狀態爲「打印作業」的通知JOB_STATUS_DELETED

我發現了類似的東西Here,但它不能解決我的問題。

我在做下一件事:

HANDLE hChange = FindFirstPrinterChangeNotification(hPrinter, 
                 PRINTER_CHANGE_ALL, 
                 0, 
                 &NotificationOptions); 

    DWORD dwChange; 
    HANDLE aHandles[2]; 
    aHandles[0] = hChange; 
    aHandles[1] = owner->GetStopRequestEvent(); 

    while (hChange != INVALID_HANDLE_VALUE) 
    { 
     // sleep until a printer change notification wakes this thread or the 
     // event becomes set indicating it's time for the thread to end. 
     WaitForMultipleObjects(2, aHandles, FALSE, INFINITE); 

     if (WaitForSingleObject(hChange, 0U) == WAIT_OBJECT_0) 
     { 
      FindNextPrinterChangeNotification(hChange, &dwChange, &NotificationOptions, (LPVOID *) &pNotification); 

      if (pNotification != NULL) 
      { 
       // if a notification overflow occurred, 
       if (pNotification->Flags & PRINTER_NOTIFY_INFO_DISCARDED) 
       { 
        DWORD dwOldFlags = NotificationOptions.Flags; 

        // we must refresh to continue 
        NotificationOptions.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH; 

        FreePrinterNotifyInfo(pNotification); 

        FindNextPrinterChangeNotification(hChange, &dwChange, &NotificationOptions, (LPVOID *) &pNotification); 

        NotificationOptions.Flags = dwOldFlags; 
       } 

       // iterate through each notification 
       for (DWORD x = 0; x < pNotification->Count; x++) 
       { 
        PRINTER_NOTIFY_INFO_DATA data = pNotification->aData[x]; 

        if (data.Type == JOB_NOTIFY_TYPE) 
        { 
         if (data.Field == JOB_NOTIFY_FIELD_STATUS) 
         { 
          if (data.NotifyData.adwData[0] & (JOB_STATUS_DELETED | JOB_STATUS_DELETING | JOB_STATUS_PRINTED)) 
          { 
           owner->SendJobsData(data.NotifyData.adwData[0]); 
          } 
       ...... 

當我刪除任務,JOB_NOTIFY_FIELD_STATUS僅傳遞刪除,並且沒有任何進一步的狀態通知,但我真的需要被刪除狀態。我究竟做錯了什麼?

輪詢方法這裏的全碼: 空隙輪詢(JobTracker的*所有者,CServiceBase *服務) { HANDLE hPrinter = NULL; HANDLE hNotification; (!OpenPrinter(owner - > GetPrinterName(),& hPrinter,NULL)) return;

PPRINTER_NOTIFY_INFO pNotification = NULL; 

    WORD JobFields[] = 
    { 
     JOB_NOTIFY_FIELD_PRINTER_NAME, 
     JOB_NOTIFY_FIELD_MACHINE_NAME, 
     JOB_NOTIFY_FIELD_PORT_NAME, 
     JOB_NOTIFY_FIELD_USER_NAME, 
     JOB_NOTIFY_FIELD_NOTIFY_NAME, 
     JOB_NOTIFY_FIELD_DATATYPE, 
     JOB_NOTIFY_FIELD_PRINT_PROCESSOR, 
     JOB_NOTIFY_FIELD_PARAMETERS, 
     JOB_NOTIFY_FIELD_DRIVER_NAME, 
     JOB_NOTIFY_FIELD_DEVMODE, 
     JOB_NOTIFY_FIELD_STATUS, 
     JOB_NOTIFY_FIELD_STATUS_STRING, 
     JOB_NOTIFY_FIELD_DOCUMENT, 
     JOB_NOTIFY_FIELD_PRIORITY, 
     JOB_NOTIFY_FIELD_POSITION, 
     JOB_NOTIFY_FIELD_SUBMITTED, 
     JOB_NOTIFY_FIELD_START_TIME, 
     JOB_NOTIFY_FIELD_UNTIL_TIME, 
     JOB_NOTIFY_FIELD_TIME, 
     JOB_NOTIFY_FIELD_TOTAL_PAGES, 
     JOB_NOTIFY_FIELD_PAGES_PRINTED, 
     JOB_NOTIFY_FIELD_TOTAL_BYTES, 
     JOB_NOTIFY_FIELD_BYTES_PRINTED 
    }; 
    PRINTER_NOTIFY_OPTIONS_TYPE Notifications[1] =     
    { 
     { 
      JOB_NOTIFY_TYPE, 
      0, 
      0, 
      0, 
      sizeof(JobFields)/sizeof(JobFields[0]), 
      JobFields 
     }, 
    }; 
    PRINTER_NOTIFY_OPTIONS NotificationOptions = 
    { 
     2, 
     PRINTER_NOTIFY_OPTIONS_REFRESH, 
     sizeof(Notifications)/sizeof(Notifications[0]), 
     Notifications 
    }; 

    // get a handle to a printer change notification object. 
    HANDLE hChange = FindFirstPrinterChangeNotification(hPrinter, 
                 PRINTER_CHANGE_ALL, 
                 0, 
                 &NotificationOptions); 

    DWORD dwChange; 
    HANDLE aHandles[2]; 
    aHandles[0] = hChange; 
    aHandles[1] = owner->GetStopRequestEvent(); 

    while (hChange != INVALID_HANDLE_VALUE) 
    { 
     // sleep until a printer change notification wakes this thread or the 
     // event becomes set indicating it's time for the thread to end. 
     WaitForMultipleObjects(2, aHandles, FALSE, INFINITE); 

     if (WaitForSingleObject(hChange, 0U) == WAIT_OBJECT_0) 
     { 
      FindNextPrinterChangeNotification(hChange, &dwChange, &NotificationOptions, (LPVOID *) &pNotification); 

      if (pNotification != NULL) 
      { 
       // if a notification overflow occurred, 
       if (pNotification->Flags & PRINTER_NOTIFY_INFO_DISCARDED) 
       { 
        DWORD dwOldFlags = NotificationOptions.Flags; 

        // we must refresh to continue 
        NotificationOptions.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH; 

        FreePrinterNotifyInfo(pNotification); 

        FindNextPrinterChangeNotification(hChange, &dwChange, &NotificationOptions, (LPVOID *) &pNotification); 

        NotificationOptions.Flags = dwOldFlags; 
       } 

       // iterate through each notification 
       for (DWORD x = 0; x < pNotification->Count; x++) 
       { 
        PRINTER_NOTIFY_INFO_DATA data = pNotification->aData[x]; 

        if (data.Type == JOB_NOTIFY_TYPE) 
        { 
         if (data.Field == JOB_NOTIFY_FIELD_STATUS) 
         { 
          if (data.NotifyData.adwData[0] & (JOB_STATUS_DELETED | JOB_STATUS_DELETING | JOB_STATUS_PRINTED)) 
          { 
           owner->SendJobsData(data.NotifyData.adwData[0]); 
          } 
         } 
         if (data.Field == JOB_NOTIFY_FIELD_STATUS_STRING) 
         { 
          int a = 0; 
         } 
        } 
        else if (data.Type == PRINTER_NOTIFY_TYPE) 
        { 
         if (data.Field == PRINTER_NOTIFY_FIELD_STATUS) 
         { 
          int a = 0; 
         } 
         if (data.Field == PRINTER_NOTIFY_FIELD_STATUS_STRING) 
         { 
          int a = 0; 
         } 
        } 
       } 
      } 

      FreePrinterNotifyInfo(pNotification); 
      pNotification = NULL; 
     } 
     else if (WaitForSingleObject(owner->GetStopRequestEvent(), 0U) == WAIT_OBJECT_0) 
     { 
      FindClosePrinterChangeNotification(hChange); 
      hChange = INVALID_HANDLE_VALUE; 
     } 
    } 

}

+0

看起來好像你做錯了什麼。你的問題是爲什麼你永遠不會收到「JOB_STATUS_DELETING」事件? – 2014-10-20 04:57:01

+0

@CareyGregory不,我需要JOB_STATUS_DELETED通知 – 2014-10-20 08:43:07

+0

實際上,它們是一回事。我不認爲你會看到DELETED狀態,因爲當作業完全刪除時,由於作業不再存在,「GetJob」將失敗。 – 2014-10-20 14:23:57

回答

2

如果任何人都面臨這樣的任務,我會離開我的方式,我解決這個問題。

我注意到,每當作業離開隊列時,PRINTER_CHANGE_JOB_DELETE通知(我的意思是改變FindNextPrinterChangeNotification)。

因此,我只是跟蹤線程所有者類(JobTracker)中的任務列表,並且每次在任何PRINTER_CHANGE_JOB上刷新它。但在刷新它之前,我看看其中的差異,如果我發現某些工作消失了(由JobId比較),我將我的工作向量,併發送到服務器失蹤的工作。

+0

你應該接受你的回答,這樣問題就會顯示爲已回答。 – 2014-10-22 04:36:09

+0

@CareyGregory感謝提醒!我想在發佈後立刻這麼做,但24小時的冷卻時間並沒有讓我這麼做 – 2014-10-22 06:43:27