James Wilcock

James Wilcock

  • NA
  • 13
  • 8.8k

Adding a webbrowser control to a form and having it run in a seperate thread?

Sep 8 2010 6:23 AM
I am trying to achieve the display of an animated gif in a windows app. I had hoped that by displaying it in a webbrowser contro that it would always be animated.

Currently it is only animated when the system is not busy however when the app is processing such as returning data the animation freezes. To prevent this wouldit be possible to get the webbrowser control to run in a different thread, and could this solve my problem?
 
However I am getting hte error
 
Cross-thread operation not valid: Control 'uctrlBusyDisplay' accessed from a thread other than the thread it was created on.
 
 
   public uctrlBusyDisplay()
        {
            InitializeComponent();
            Thread t = new Thread(new ThreadStart(DoIt));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();           

         
        }
private void DoIt()
         {
             WebBrowser wb = new WebBrowser();
             wb.Location = new Point(20, 10);
             wb.Navigate("www.google.co.uk");
             this.Controls.Add(wb);
         
           

         }
 
The error seems to occur when I try and add the webbrowser control to the usercontrol.
this.Controls.Add(wb);
 Action a = () => { Controls.Add(wb); 
};
           
            Invoke(a);

I have triebut still get the error as I dont really understand the method.
Also jumping ahead to when I have achieved this when I have added the control will the webbrowser still control be running in its own thread when it is added to the main form?

Answers (11)