我正在向打印机发送打印作业,我试图跟踪它们的状态,以便在我自己的队列中反映它。
似乎打印作业从未经过"已打印"、"已完成"状态。
我做了一个小的测试程序,看看是否可能我使用。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.
我如何知道文档处于"已完成"状态?
除非您启用打印机的"Keep列印文件"选项,否则您永远不会看到Completed
状态,因为列印作业一旦完成就会被删除。转变为Printing-->Deleting,
而不是Printing-->Completed-->Deleting
因此,您有两个可能的信号表示作业已经完成:1)当它进入Deleting
状态时和2)当它从队列中消失时。据我所知,没有办法区分打印作业被删除是因为它完成了,还是因为某人或其他程序删除了它。
我只是在测试一个类似的情况,并得出以下日志输出,没有 'Keep列印文件'标志集:
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;
}
}
}
}
}