views:

243

answers:

1

Why does this work,

 private void buttonBoo_Click(object sender, EventArgs e)
 {
  GeckoBrowser.Navigate("http://www.google.com/");
 }

and this not?

 private void buttonBoo_Click(object sender, EventArgs e)
 {
  Thread thread = new Thread(delegate()
  {
   GeckoBrowser.Navigate("http://www.google.com/");
  });

  thread.Start();
 }
+5  A: 

GeckoBrowser is a Windows Forms Control. A Control's properties and methods may be called only from the thread on which the Control was created. To do anything with a Control from another thread, you need to use the Invoke or BeginInvoke method, e.g.

Thread thread = new Thread(delegate()
{
  Action<string> action = url => GeckoBrowser.Navigate(url);
  GeckoBrowser.Invoke(action, new object[] { "http://www.google.com/" });
});
itowlson