1

我正在向打印机发送打印作业,并尝试跟踪它们的状态,以便将其反映在我自己的队列中。

似乎打印作业从未经历过“已打印”、“已完成”状态。

我做了一个小测试程序,看看我是否以错误的方式使用了 .net 对象,但这仍然发生。我运行我的测试程序来查询一个新的打印作业,一旦有一个打印作业,我就会对其进行大量轮询并刷新每次以查看它的状态并确保我不会错过任何东西。

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);
}

即使打印了文档,我得到的结果是:

**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.

我如何知道文档何时处于“已完成”状态?

4

2 回答 2

3

除非您为打印机启用“保留打印的文档”选项,否则您永远不会看到Completed状态,因为打印作业一完成就会被删除。过渡Printing-->Deleting,不是Printing-->Completed-->Deleting

因此,您有两个可能的信号表明作业已完成:1)当它进入Deleting状态时和 2)当它从队列中消失时。据我所知,没有办法区分打印作业是因为它完成而被删除,还是因为一个人或其他程序删除了它。

于 2013-05-13T20:56:51.153 回答
1

我只是在测试类似的情况,并在没有设置“保留打印文档”标志的情况下跟踪日志输出:

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.

似乎它可能取决于打印机/驱动程序。

用于获取此输出的代码:

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;
                }
            }
        }
    }
}
于 2017-11-24T11:21:15.037 回答