我有一个使用GhostScript.NET打印PDF文档的功能。如果一个用户尝试打印,则一切正常,但是当多个用户尝试同时打印时,它不会打印所有文档。我认识到我将创建多个ghostscript实例以实现多个同时打印。我遇到了这个问题:
Ghostscript.NET Multithreading Issue
其中一位用户说要创建一个并行的forloop,但我老实说不确定如何构造一个以便创建正确数量的GhostScript实例。
我有主要功能:
public static void PrintFormPdfData(byte[] formPdfData, string printer)
{
var filename = @"C:\TEMP\report.pdf";
using (var file = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
{
file.Write(formPdfData, 0, formPdfData.Length);
using (GhostscriptProcessor processor = new GhostscriptProcessor())
{
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dPrinted=false");
switches.Add("-dBATCH");
switches.Add("-dPDFFitPage");
switches.Add("-dNOPAUSE");
switches.Add("-dNOSAFER");
switches.Add("-dNumRenderingThreads=3");
switches.Add("-dNumCopies=1");
switches.Add("-sDEVICE=mswinpr2");
switches.Add("-sOutputFile=%printer%" + printer);
switches.Add(filename);
try
{
processor.StartProcessing(switches.ToArray(), null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
File.Delete(filename);
}
}
}
}
非常感谢任何帮助。
答案 0 :(得分:0)
我想出了如何使用Parallel ForEach ...这是我更新的代码,适用于对运行多个ghostscript实例感兴趣的人:
public static void PrintingQueue(Queue<byte[]> printQueue, string printer, int copies)
{
Parallel.ForEach(printQueue, (currentFile) =>
{
currentFile = printQueue.Dequeue();
PrintFormPdfData(currentFile, printer, copies);
});
}
private static void PrintFormPdfData(byte[] pdfFormBytes, string printer, int copies)
{
var fileName = Path.GetTempFileName();
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
file.Write(pdfFormBytes, 0, pdfFormBytes.Length);
using (GhostscriptProcessor processor = new GhostscriptProcessor())
{
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dPrinted");
switches.Add("-dBATCH");
switches.Add("-dPDFFitPage");
switches.Add("-dNOPAUSE");
switches.Add("-dNOSAFER");
switches.Add("-dNumRenderingThreads=3");
switches.Add("-dNumCopies=1");
switches.Add("-sOutputFile=%printer%" + printer);
switches.Add("-sDEVICE=mswinpr2");
switches.Add(fileName);
try
{
processor.StartProcessing(switches.ToArray(), null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
file.Close();
}
File.Delete(fileName);
}
}