locked
Another Control.Invoke RRS feed

  • Question

  • I am confused about the error I am getting trying to use Control.Invoke in the following. I thought "this.Invoke" for a class derived from Form would work. This is CF2.0
    Steve

    public partial class AssetTracker : Form {
    ...
    public void setWebBrowser(WebBrowser newBrowser) { // called from another thread
    // save status in local variable
          browser = newBrowser;
    // the following causes error: "Control.Invoke must be used to interact with controls created on a separate thread"
          this.Invoke(new EventHandler(workerAddBrowser));
    }

     private void workerAddBrowser(object sender, EventArgs e) {
                Controls.Add(browser);
    }

    Friday, August 25, 2006 9:59 PM

Answers

  • Controls must be created on UI thread. So you can't do what you doing but you can do something like this:

    public partial class AssetTracker : Form {
    ...
    public void setWebBrowser() { // called from another thread
          this.Invoke(new EventHandler(workerAddBrowser));
    }

     private void workerAddBrowser(object sender, EventArgs e) {
                browser = new WebBrowser();

                Controls.Add(browser);
    }

    Friday, August 25, 2006 10:15 PM

All replies

  • Controls must be created on UI thread. So you can't do what you doing but you can do something like this:

    public partial class AssetTracker : Form {
    ...
    public void setWebBrowser() { // called from another thread
          this.Invoke(new EventHandler(workerAddBrowser));
    }

     private void workerAddBrowser(object sender, EventArgs e) {
                browser = new WebBrowser();

                Controls.Add(browser);
    }

    Friday, August 25, 2006 10:15 PM
  • Thanks. That got rid of the exception. What is the significance of moving "brower = .." to the delegate?

    One more question. In the same AssetTracker class I have the following that works splendidly. Why does this work and set WebBrowser did not?

    private System.Windows.Forms.TextBox statusLabel;
    ...
    public void setStatus(String newStatus) {
    // save status in local variable
    statusMessage = newStatus;
    // and invoke control delegate to update UI form
    statusLabel.Invoke(new EventHandler(workerStatusUpdate));
    }
    String statusMessage;
    // form control update delegate
    private void workerStatusUpdate(object sender, EventArgs e) {
    statusLabel.Text = statusMessage;
    }

    Friday, August 25, 2006 10:46 PM
  •  seu wrote:
    Thanks. That got rid of the exception. What is the significance of moving "brower = .." to the delegate?

    One more question. In the same AssetTracker class I have the following that works splendidly. Why does this work and set WebBrowser did not?

    private System.Windows.Forms.TextBox statusLabel;
    ...
    public void setStatus(String newStatus) {
    // save status in local variable
    statusMessage = newStatus;
    // and invoke control delegate to update UI form
    statusLabel.Invoke(new EventHandler(workerStatusUpdate));
    }
    String statusMessage;
    // form control update delegate
    private void workerStatusUpdate(object sender, EventArgs e) {
    statusLabel.Text = statusMessage;
    }



    Nevermind. It sunk in!!
    Friday, August 25, 2006 11:10 PM