none
Why to a backgroundWorker have not permission RRS feed

All replies

  • Please show your code, Noam. Where are you trying to update the label text? In the DoWork() event handler or in the RunWorkerCompleted() event handler?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 23, 2018 4:59 PM
    Moderator
  • I'm going to add to my previous reply, without seeing the code you're using.

    When using a BackgroundWorker, you should only be updating your UI Controls in either the ProgressChanged() event handler or the RunWorkerCompleted() event handler. I'm getting together an example for you, but I won't be able to post it for you for a few hours (I have some other things that I have to do in a few minutes, so I don't have time to finish my example right now).


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 23, 2018 5:53 PM
    Moderator
  • Thats says "//The operation between threads is invalid. The control 'label1' comes from a sub procedure other than the thread in which it was created//"

    Sunday, December 23, 2018 6:38 PM
  • Unfortunately Noam, I can't see what method you're in when you get the error. Screenshots aren't usually a good way to show your code, or your Exceptions. When posting code, click the "Insert Code Block" icon that looks like this "< >", the second icon from the right at the top of the Reply window.

    As I said in my previous reply, you should only be updating your UI Controls in either the ProgressChanged() event handler or the RunWorkerCompleted() event handler. This error must mean that you're not using one of those two event handlers.

    private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.DoWork += bw_DoWork;
        backgroundWorker1.RunWorkerCompleted += bw_RunWorkerCompleted;
        backgroundWorker1.RunWorkerAsync(args);
    }
    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        label1.Text = "hfg";
    }

    If I've made a bad assumption about what that hidden method is, then post some code using the "Insert Code Block" button.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, December 23, 2018 9:48 PM
    Moderator
  • The error message is pretty clear, isn't it?  This is a fundamental restriction of all of the major windowing systems, not just Windows.  All actions on a window have to be done from the thread that created the window, which is usually the main thread.  This is why Windows Forms windows have the "Invoke" method.  It's sole purpose is to call a UI function on the correct thread.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, December 24, 2018 2:52 AM