WinForms打印中的多线程问题

时间:2014-04-18 09:28:49

标签: c# .net multithreading winforms printing

我正在使用WinForms WebBrowser在我的应用程序中打印它的内容。但重点是我希望打印在其他线程中完成,因为它可能很长,并且我得到线程错误(:ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'无法实例化,因为当前线程是不是在单线程公寓里)。这是代码:

printing.frmPrinting frm = new printing.frmPrinting();
Thread tprint = new Thread(delegate() { frm.PrintHelpPage(_htmlcontent); });
tprint.Start();

打印机功能:

public void PrintHelpPage(string _content)
    {
        // Create a WebBrowser instance. 
        WebBrowser webBrowserForPrinting = new WebBrowser();

        // Add an event handler that prints the document after it loads.
        webBrowserForPrinting.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(PrintDocument);

        // Set the Url property to load the document.
        webBrowserForPrinting.DocumentText = _content;
    }

    private void PrintDocument(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {

        ClearBrowserPrintHeaderAndFooter();
        // Print the document now that it is fully loaded.
        ((WebBrowser)sender).Print();
        // Dispose the WebBrowser now that the task is complete. 
        ((WebBrowser)sender).Dispose();
    }

0 个答案:

没有答案