1

For some reason, ToolStrip's ProgressBar and Label are not updated from other thread. After a lot of reading, I came up with this, but still not working (button is OK however, but is not on status bar).

    public void GdmReaderMessageEvent(object sender, GdmMessagesEventArgs e)
    {
        Console.WriteLine(e.Message);

        // error or abort
        if (e.Message.StartsWith("Error:") || e.Message.StartsWith("Aborted"))
        {

            // cross thread crap
            if (pb.GetCurrentParent().InvokeRequired)
                pb.GetCurrentParent().Invoke(new MethodInvoker(delegate { pb.Visible = false; }));

            if (lblStatus.GetCurrentParent().InvokeRequired)
                lblStatus.GetCurrentParent().Invoke(new MethodInvoker(delegate { lblStatus.Text = e.Message; }));

            btnImport.Invoke(new MethodInvoker(delegate { btnImport.Text = "Import"; }));

            //lblStatus.Text = e.Message;
            //btnImport.Text = "Import";
            //pb.Visible = false;
        }
    }

Edited, all function now, but this is just a callback from a BGW thread, I thought it's already obvious. If I use just that:

        lblStatus.Text = e.Message;  // this is a Label inside Status
        btnImport.Text = "Import";   // this is just a button on Form
        pb.Visible = false;          // this is a Progress Bar inside Status    

Cross thread error will be thrown. So that's why I used with Invoke(). Now the button CAN BE updated, but all controls from Status Bar not.

Hope is more clear now.

I use .NET 3.5, VS2008, Winforms. Any solution?

yo3hcv
  • 1,531
  • 2
  • 17
  • 27
  • what's your platform? winforms? wpf? webforms? please tag appropriately – rory.ap May 09 '17 at 13:06
  • 2
    Is it hitting the conditions? I know it's a weird question, but I don't see the whole function, so perhaps you're already invoking, or it's not required for whatever reason... You say button is always working - button isn't checked for InvokeRequired... The 2 that are broken are. – Aaron May 09 '17 at 13:12
  • 1
    Aaron has probably answered your question. I would just like to add, you don't have to invoke every control separately. An invoke makes sure the containing code is executed on the same thread as the control was made on. You reference your controls by name so you are most likely inside your form, a form is also a control, therefor you could simplify it into a single `this.Invoke(...)` and put all the GUI updating code in there. – Fixation May 09 '17 at 13:21
  • @Aaron, Fixation, see edited, hope this is more clearly now :) – yo3hcv May 09 '17 at 14:07

1 Answers1

0

Ok , I found it. @Fixation, thanks for guide me with "this" (I mean Form itself). I was trying to use invoke of control which not worked.

Also, this post was useful unable to update progress bar with threading in C#

        this.BeginInvoke((Action)(() => pb.Visible = false));
        this.BeginInvoke((Action)(() => lblStatus.Text = e.Message));

Just perfect, thanks.

Community
  • 1
  • 1
yo3hcv
  • 1,531
  • 2
  • 17
  • 27