1

I Have try this. https://stackoverflow.com/a/142069/2126472

like toolStripStatusLabel1.InvokeRequired

but toolStripStatusLabel1 has no InvokeRequired

and I has try this too. https://stackoverflow.com/a/15831292/2126472

and it error like invalid argument on

SetTextCallback d = new SetTextCallback(SetText); form.Invoke(d, new object[] { form, ctrl, text });

when I use ThreadHelperClass.SetText(this, toolStripStatusLabel1, "This text was set safely.");

but not error when I use textBox

My code have method that wait for bool from another method that use Thread to run in background.

Thread t = new Thread(TestRemoteDB);
t.Start();
// In TestRemoteDB() will call updateRemoteDBStatus()

like this

private void updateRemoteDBStatus(bool successful)
        {
            if (successful)
            {
                toolStripStatusLabel4.Text = "OK!";
                toolStripStatusLabel4.ForeColor = Color.Green;
            }
            else
            {
                toolStripStatusLabel4.Text = "Error!";
                toolStripStatusLabel4.ForeColor = Color.Red;
            }
        }
Community
  • 1
  • 1

1 Answers1

6

Try this:

this.BeginInvoke((Action)(() => toolStripStatusLabel1.Text = "This text was set safely."));
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
  • 2
    That won't work because a ToolStripStatusLabel doesn't have a BeginInvoke method. And even if it did it wouldn't make sense because ThreadHelperClass is already using Invoke to execute code on the UI thread. And it won't compile since the ToolStripStatusLabel is not a Control and thus can't be used with the ThreadHelperClass linked by the OP. – Dirk Mar 12 '15 at 11:32
  • Lapsus, I meant "this" and wrote "toolStripStatusLabel". Tnx. – Marko Juvančič Mar 12 '15 at 11:34
  • I think this is work and save me a lot line of code. Thank you very much @Marko Juvančič. – Kanin Peanviriyakulkit Mar 12 '15 at 11:41