I've a realtime wpf application and sometimes I need to printout a document on a LAN printer. This is a part of my code:
using (PrintDocument prnDocument = new PrintDocument())
{
prnDocument.PrintPage += new PrintPageEventHandler(prnDocument_PrintPage);
if (!string.IsNullOrEmpty(printFile))
{
ImageFormat format = ImageFormat.Png;
float scale = 1; // Convert.ToSingle(_Scale.Value) / 100f;
long quality = 75; // Convert.ToInt64(_Quality.Value);
//string output = ""; // _TextBoxOutput.Text;
PaperSize ps = new PaperSize("label", panel.Width, panel.Height);
panel.BorderStyle = panelBorder ? BorderStyle.FixedSingle : BorderStyle.None;
prnDocument.DefaultPageSettings.PaperSize = ps;
prnDocument.DefaultPageSettings.Margins.Left = 0;
prnDocument.DefaultPageSettings.Margins.Top = 0;
PrintController controller = new PrintControllerFile(format, scale, quality, printFile);
prnDocument.PrintController = new PrintControllerWithStatusDialog(controller, "Exporting");
}
else
{
PaperSize ps = new PaperSize("label", panel.Width, panel.Height);
panel.BorderStyle = panelBorder ? BorderStyle.FixedSingle : BorderStyle.None;
prnDocument.DefaultPageSettings.PaperSize = ps;
prnDocument.DefaultPageSettings.Margins.Left = 0;
prnDocument.DefaultPageSettings.Margins.Top = 0;
if (!string.IsNullOrEmpty(printerName))
prnDocument.PrinterSettings.PrinterName = printerName;
prnDocument.PrintController = new StandardPrintController();
}
prnDocument.Print();
}
Basically each time the Print() is invoked, my application freeze up till 2-3 seconds, then it resume. I think that during that time I send data to the print spooler. I've also tryied to use a thread:
System.Threading.Tasks.Task.Run(() => { prnDocument.Print(); });
But nothing seems to change. Any advice?