WPF在后台打印文档

WPF在后台打印文档,wpf,printing,background-thread,Wpf,Printing,Background Thread,我有一个实时wpf应用程序,有时我需要在局域网打印机上打印一个文档。这是我代码的一部分: using (PrintDocument prnDocument = new PrintDocument()) { prnDocument.PrintPage += new PrintPageEventHandler(prnDocument_PrintPage); if (!string.IsNullOrEmpty(printFile)) { ImageFormat

我有一个实时wpf应用程序,有时我需要在局域网打印机上打印一个文档。这是我代码的一部分:

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();
}   
基本上每次调用Print()时,我的应用程序都会冻结到2-3秒,然后恢复。我认为在这段时间内,我会将数据发送到后台打印程序。 我还尝试使用一个线程:

System.Threading.Tasks.Task.Run(() => { prnDocument.Print(); });

但一切似乎都没有改变。有什么建议吗?

“但似乎什么都没有改变。”所以,当你使用
任务。运行
时,你的UI线程到底挂在哪里2-3秒?很有趣。。。我检查了代码,在一个控制器文件中发现了ad ONEDPAGE事件,该文件用于将文档打印为图像。我需要调查(我正在处理遗留代码)