.netsystem.printingprint-spooler-api

System.Printing skips a few job status states?


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?


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.