Question

I am sending print jobs to the printer and I try to keep track of their status in order to reflect it in a queue of my own.

It seems as if the print jobs never go through "Printed", "Completed" status.

I made a small test program to see if maybe I was using the .net object the wrong way but this still happened. I ran my test program to query for a new print job and once there was a one I heavily polled it and refreshed every time to see its status and make sure I don't miss anything.

PrintServer ps = new PrintServer(@"\\printServer");
PrintQueue pq = new PrintQueue(ps, "PDF Writer - bioPDF");
PrintSystemJobInfo jobInfo = null;

while (jobInfo == null)
{
    pq.Refresh();
    var printJobs = pq.GetPrintJobInfoCollection(); 

    foreach (var printJob in printJobs)
    {
        printJob.Refresh();

        jobInfo = printJob;
        break;
    }

    Thread.Sleep(100);
}

PrintJobStatus jobStatus = PrintJobStatus.None;
PrintJobStatus prevJobStatus = PrintJobStatus.None;
while (true)
{
    try
    {
        jobInfo.Refresh();
    } catch { }

    prevJobStatus = jobStatus;
    jobStatus = jobInfo.JobStatus;

    if (jobStatus != prevJobStatus)
    {
        Console.WriteLine("**UPDATE**");
        SpotTroubleUsingProperties(jobInfo); // Microsoft example method that prints a description of each possible job status
    }

    Thread.Sleep(20);
}

The result I get, even though the document is printed, is:

**UPDATE**
The job is spooling now.
**UPDATE**
The job is printing now.
**UPDATE**
The user or someone with administration rights to the queue has deleted the job. It must be resubmitted.

How can I know when the document is in "Completed" status?

Was it helpful?

Solution

Unless you enable the "Keep printed documents" option for the printer, you're never going to see the Completed status since print jobs are deleted as soon as they finish. The transition is Printing-->Deleting, not Printing-->Completed-->Deleting.

So you have two possible signals that a job has completed: 1) When it enters the Deleting status and 2) when it vanishes from the queue. To my knowledge, there is no way to distinguish between a print job being deleted because it completed and because a person or other program deleted it.

OTHER TIPS

I was just testing a similar situation and came to following logging output, without the 'Keep printed documents' flag set:

Info: 11/24/2017 12:08:18 PM - JobStatus for document testA4.pdf is Printing, Retained.
Info: 11/24/2017 12:08:22 PM - JobStatus for document testA4.pdf changed from Printing, Retained to Retained.
Info: 11/24/2017 12:08:22 PM - JobStatus for document testA4.pdf changed from Retained to Completed, Retained.
Info: 11/24/2017 12:08:22 PM - Document testA4.pdf marked as Completed by printer.
Info: 11/24/2017 12:08:33 PM - JobStatus for document testA4.pdf changed from Completed, Retained to Deleted.

Seems that it may be printer/driver dependent.

Code used to get this output:

public void MonitorPrintJobStatus(string printerName, Document document, CancellationToken token)
{
    using (var printServer = new LocalPrintServer())
    {
        using (var printQueue = printServer.GetPrintQueue(printerName))
        {
            foreach (var printJobInfo in printQueue.GetPrintJobInfoCollection())
            {
                if (printJobInfo.Name == document.DocumentToBePrinted.Path)
                {
                    bool printed = false;
                    var previousPrintJobStatus = printJobInfo.JobStatus;
                    _logger.LogInfo($"{DateTime.Now} - JobStatus for document {document.DocumentToBePrinted.Path} is {previousPrintJobStatus}.");
                    while (!token.IsCancellationRequested && !printed)
                    {
                        printJobInfo.Refresh();
                        if (previousPrintJobStatus != printJobInfo.JobStatus)
                        {
                            _logger.LogInfo($"{DateTime.Now} - JobStatus for document {document.DocumentToBePrinted.Path} changed from {previousPrintJobStatus} to {printJobInfo.JobStatus}.");
                            previousPrintJobStatus = printJobInfo.JobStatus;
                        }
                        if (printJobInfo.IsCompleted)
                        {
                            _logger.LogInfo($"{DateTime.Now} - Document {document.DocumentToBePrinted.Path} marked as Completed by printer.");
                            document.Status = DocumentPrintStatus.Printed;
                            printed = true;
                        }
                    }
                    break;
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top