-1

I have a code and when you click on start some controls must be disabled for the safety... :

Thread t = new Thread(() => MyMethod(text));

button1.Enabled = false;
textBox2.Enabled = false;
button2.Enabled = false;

t.Start();

This is not a problem. My problem is where i should turn all of them On again, My mean is when the MyMethod ends , i have to Invoke all the controls i want to enable them.

MyMethod(string text){
.
.
.
button1.Invoke(new MethodInvoker(delegate{ button1.Enabled=true; }));
textBox2.Invoke(new MethodInvoker(delegate{textBox2.Enabled=true; }));
button2.Invoke(new MethodInvoker(delegate{ button2.Enabled=true; }));
}

So if we had a bunch of Controls Set, It would be hard to change the values of they in a thread.

Is there anyway to Access these for an easier or faster way ?

Thanks,

alix54
  • 444
  • 1
  • 5
  • 13
  • https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c?rq=1 – KamikyIT Jul 07 '17 at 12:14
  • 1
    Possible duplicate of [How to update the GUI from another thread in C#?](https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – JensG Jul 07 '17 at 13:24

1 Answers1

1

Store the current dispatcher before calling your method in a separate thread.

var uiDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;

Use Dispatcher in your MyMethod such as:

void MyMethod(Dispatcher uiDispatcher)
{
...

    uiDispatcher.Invoke(() =>
                {
    button1.Enabled = true;
    button2.Enabled = true;
    ...
                });
}
Martin Ch
  • 1,337
  • 4
  • 21
  • 42
  • you need a reference to WindowsBase.dll and framework version 3+ – Martin Ch Jul 07 '17 at 12:38
  • So i found it but when i run my program , an error appears and says : `Cross-thread operation not valid: Control "button1" accessed from a thread other than the thread it was created on` – alix54 Jul 07 '17 at 12:50
  • Sorry but i dont have any `Dispatcher` in the `Application` – alix54 Jul 07 '17 at 13:04
  • @alix54 The code you showed in your question is winforms code, not WPF code, and so as a result this is the completely wrong framework for what you're using. – Servy Jul 07 '17 at 13:11
  • @Servy What bad things happens if i use this ? – alix54 Jul 07 '17 at 13:12
  • @alix54 If you completely scrap your entire existing codebase and create a new project with the correct framework, then nothing, it's an entirely appropriate tool if you're actually writing code using this framework. If you're not, actually writing a WPF program, or somehow try to use two disjointed frameworks at the same time I couldn't even predict, but there's no way that that ends well. – Servy Jul 07 '17 at 13:15
  • @Servy So what code you prefer to use in this case ? – alix54 Jul 07 '17 at 13:17
  • 1
    @alix54 You've got the code to marshal to the UI thread right in the question using the proper framework. There's no reason to add in an entirely separate framework just to have access to an identical method. That said, the whole program can be radically improved by creating a `Task` and awaiting it. – Servy Jul 07 '17 at 13:19