Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:20+00:00 2026-06-13T23:09:20+00:00

I’m trying to create GUI program that generates HTML invoices, and sends them for

  • 0

I’m trying to create GUI program that generates HTML invoices, and sends them for printing.
I have this working. However, now I want to introduce threading.

I have a form with a BackgroundWorker. The Background worker runs this code:

#region BackGroundWorker   
private void bg_htmlGeneration_DoWork(object sender, DoWorkEventArgs e)
{
   //SOME MORE CODE..

   foreach (XElement ele in Lib.GetInvoiceElement(inv, ico.Supplier))
   {
      PrintDocument(Lib.CreateHTMLFile());
   }
}
#endregion  


public void PrintDocument(string fileName)
{
    var th = new Thread(() =>
    {
        WebBrowser webBrowserForPrinting = new WebBrowser();
        webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocumentHandler);
        webBrowserForPrinting.Url = new Uri(fileName);
        Application.Run();
    });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}

public void PrintDocumentHandler(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).Print();
    ((WebBrowser)sender).Dispose();
    Application.ExitThread();
}

Everything runs through fine. However, the WebBrowser object refuses to print. There are no errors (that are obvious), the program finishes off with nothing sent to the printer.
When I take away the threading, the program works again.

My knowledge of threading is weak, and I’m pretty much teaching myself – so presumably I’m misunderstanding how threading priority is set.

Here’s How it should work:

  1. User selects Invoice(s) on Main Form, chooses to print.
  2. Background thread goes away and prints them while user continues on the program.

Any ideas would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:09:21+00:00Added an answer on June 13, 2026 at 11:09 pm

    Main problem with your code is WebBrowser wrong using.

    WebBrowser supposed to be used for interactive web-browsing, during it user do some things in the internet. But in your case you are using WebBrowser just for the printing after downloading the html. This is wrong by two reasons:

    1. Your code creates whole Windows Forms Control and not using even half of its functionality.
    2. Your code tries to use the WinForms Control in the background thread, which leads to the unexpected behaviour.

    BackgroundWorker class supposed to be used for

    execute a time-consuming operation (like downloads and database transactions) in the background.

    Much more:

    You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

    Your code will fail in the background thread, because WinForms control is a user-interface object.

    Just for the record, WebBrowser.Print method invokes native windows API, so you have no chance that this will work in background. From the disassembly code:

    this.AxIWebBrowser2.ExecWB(NativeMethods.OLECMDID.OLECMDID_PRINT, 
      NativeMethods.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 
      ref obj, 
      IntPtr.Zero);
    

    So, my suggestion for your code is:

    1. Remove usage of the WebBrowser class in the background. Use HttpWebRequest instead for downloading the web content.
    2. Choose other way for the printing your downloaded content. Options are:
      • PrintDocument implementation (example for it is here).
      • Use MS Office classes for opening and printing your html-files (first parameter for the Print method is a Boolean Background, I think this can help you). As far as I know, even in 2010 this approach works well.
      • Check other questions about printing (here is a discussion of printing the images, but this is the same thing, I think).

    PS: in the comments you’ve said that you may need the PDF from your html. I did this by C# by two ways:

    • Batching using the wkhtmltopdf
    • Using Microsoft Office Add-in: Microsoft Save as PDF or XPS.
      This Add-in should be installed on the server, after that you can easily use MS Office classes for saving the output in the PDF format.

    Some update here:

    As we have an async/await and TPL options for the time-consuming operations, I don’t recommend you to use the BackgroundWorker class anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.