I am using WinForms WebBrowser to print the content of it in my application. But the point is that I want the printing be done in other thread, as it might be long, and i am getting threading error (: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment). Here is the code:
printing.frmPrinting frm = new printing.frmPrinting();
Thread tprint = new Thread(delegate() { frm.PrintHelpPage(_htmlcontent); });
tprint.Start();
The printer function:
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();
}