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

  • SEARCH
  • Home
  • 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 8691639
In Process

The Archive Base Latest Questions

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

I am having problem using webbrowser control to correctly display html. My goal is

  • 0

I am having problem using webbrowser control to correctly display html. My goal is to add custom html to a webbrowser control, have it displayed, and save the screenshot of that as png. Currently I am using Document.OpenNew and Document.Write(htmlText) and Application.DoEvents(). However since I am running this in a background thread, sometimes it deadlocks. I know the culprit is Application.DoEvents() which is giving me troubles.

However, if I remove that and set the html directly to DocumentText property, how do I know when it is fully “rendered” or loaded. I used the DocumentCompleted Event but that does not seem to work since the image that is saved is still empty even after the event fires.

I also have the thread as STA.

Here is the existing code:

Thread th = new Thread(new ThreadStart(createImage));
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join(TIMEOUT);

private void createImage() {
 var browser = new WebBrowser();
 var doc = browser.Document;
 doc.OpenNew(false);
 doc.Write("<html><body>....</body><html>)");
 //loop for few seconds
 for(int i=0; i<20; i++)
 {
   Application.DoEvents();
   Thread.Sleep(100);
 }
 //save to file as png.
}

Here is the code I am trying:

private void createImage() {
 var browser = new WebBrowser();
 bool docComplete = false;
 browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(
                (Object sender, WebBrowserDocumentCompletedEventArgs args) =>
                { docComplete = true; }
                );
 browser.DocumentText = "<html>.....";
 while (!_docComplete)
 {                    
    Thread.Sleep(100);
  }
 // save image
 // :-( not working
}
  • 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-13T00:09:41+00:00Added an answer on June 13, 2026 at 12:09 am

    You have to create thread as STA, that is only the way to run WebBrowser in a background thread.

    bw.SetApartmentState(ApartmentState.STA);
    

    There is no information about waiting in HtmlDocument.Write Method. If is yet required then you can add DocumentCompleted handler instead of DoEvents

    private void captureImageThread(string html) { 
       var thread = new Thread(() => { 
           var browser = new WebBrowser(); 
           browser.DocumentCompleted += browser_DocumentCompleted; 
           browser.DocumentText = html;
           Application.Run(); 
       }); 
       thread.SetApartmentState(ApartmentState.STA); 
       thread.Start(); 
    } 
    
    void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { 
       var br = sender as WebBrowser; 
       // save image here
       Application.ExitThread();   // Stops the thread 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem using custom components at runtime. I have this custom FlowLayoutPanel
I'm having a problem with using this in js. I have a function that
I am having a problem using $html->link helper in my view. Consider this snippet...
Hello everyone. I'm using Silverlight webbrowser control, and I'm having some problems with that.
I am using the ASP.NET AJAX Control Kit and I am having a problem
I am having some problem using the clone function in jQuery. I have been
Im having a problem using Email.SendMail(). I need to add more details like Event_Location,
I am having problem in using multilevel inheritance as follows. I have a top
I am using <webbrowser>.Document.Window.Frames to get frames. My <WebBrowser> having 2 frames.My only problem
I have been having problem using phpmailer to send bulk emails, no problem with

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.