0

I want to get informed when a print job is completed using C#

I'm uisng C# Winform.I wonder if there is a way that an event can be raised when a print job is completed in a specified print queue.I have tried the following solutions:

I'm current polling queries from WMI. It's unreliable because it depends on the interval of the query. Sometimes the system deletes the data entry in WMI database after the print job is completed and with a long query interval I could miss a print job, while with a short interval I could put too much extra load.

I also used PrintQueue.GetPrintJobInfoCollection() which returns an array of PrintSystemJobInfo that raises no event.I have to query their status periodically and leads to the same problem as I mentioned above.

Though I don't think my messy code is requeired here but i will paste it anyway.

public static PrintQueue GetPrintQueue(string printerName)
        {
            PrintQueue targetPrintQueue = null;
            using (LocalPrintServer printServer = new LocalPrintServer())
            {
                PrintQueueCollection printQueuesOnLocalServer =
                    printServer.GetPrintQueues(new[] {EnumeratedPrintQueueTypes.Local});
                targetPrintQueue =
                    (from PrintQueue printQueue in printQueuesOnLocalServer
                        where printQueue.Name == printerName
                        select printQueue).FirstOrDefault();
            }

            return targetPrintQueue;
        }

public static List<PrintSystemJobInfo> GetPrintJobs(string printerName)
        {
            PrintQueue targetPrintQueue = GetPrintQueue(printerName);
            if (targetPrintQueue != null)
            {
                targetPrintQueue.Refresh();
                return targetPrintQueue.GetPrintJobInfoCollection().ToList();
            }
            else
            {
                throw new Exception();
            }
        }

MORE INFO

I read FindNextPrinterChangeNotification very carefully (at least i think that i'm careful enough) and found no bit indicating a print job is done in pdwChange, if this api do meet the requirement I described above, would someone kindly provide me more detail on the realization or an example or something?

1 Answers1

0

In order to achieve event handling for printing you should use FindFirstPrinterChangeNotification function.

pilot13
  • 36
  • 3
  • But I found no bit indicating a print job is done in pdwChange, and I can't find something like a job id in the return value to retrieve the status of the job. Am I missing something? – Ragnarokkr Xia Aug 27 '19 at 03:21