-1
private void btnGetPath_Click(object sender, EventArgs e)
{
    Thread thread1 = new Thread(() => Allfunction());
    thread1.Start();
    txtCmd.AppendText("\n thread of getFiles() start");
}

private void Allfunction(object sender, EventArgs e)
{    
    txtCmd.Invoke(new Action( () =>
    { 
        txtCmd.AppendText("\n hello world");
    }));
}

When I was going for threading text and label are User Interface Thread so I invoke this to change into multi thread please give me help for making a function for all text and label therefore I will help me to take a function only not write full code for invoke.

Tom Schardt
  • 470
  • 1
  • 7
  • 18

1 Answers1

-2

You can't. UI elements can only be modified inside UI thread. What you can do is create a delegate that will be invoked on UI thread.

https://www.codeproject.com/Articles/52752/Updating-Your-Form-from-Another-Thread-without-Cre

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – mrogal.ski Sep 08 '17 at 10:32
  • @m.rogalski The answer is "what you want can't be done", the link merely provide an alternative way to implement it. I think you're taking the rule too far away.. – Oscar Sep 08 '17 at 10:35
  • If the answer is _"what you want can't be done"_ then it's an incorrect answer because with programming you can do basically anything. – mrogal.ski Sep 08 '17 at 10:37
  • It depends on the underlaying UI Framework. If it's `WinForms` then simply by calling `Invoke` on that control, or if it's `WPF` then use a `Dispatcher.Invoke`. But since you're a _"Senior IT consultant, mostly with Microsoft technology"_ you should know that. – mrogal.ski Sep 08 '17 at 10:45
  • 1
    @Oscar `new Thread(() => { textBox1.Invoke((Action) (()=>textBox1.AppendText("new Text"))); }).Start();` That is all. – L.B Sep 08 '17 at 10:48
  • 1
    @m.rogalski Of course I know. The one who doesn't seem to know that Control.Invoke or Dispatcher.Invoke executes a delegate on the UI thread, not in any other one, is you. – Oscar Sep 08 '17 at 10:49