0

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?

  • "*But nothing seems to change.*" So, when you use `Task.Run`, where *exactly* does your UI thread hang for 2-3 seconds? – Heinzi Sep 26 '19 at 08:53
  • Interesting... I checked the code, and I've found ad OnEndpage event inside a Controller file which is used to print my document as image. I need to investigate (I'm handling a legacy code) –  Sep 26 '19 at 09:12

0 Answers0