What is the best practice to invoke a method in a different thread from a winform button so the ui doesn't freeze or creates a delay?
Asked
Active
Viewed 7,203 times
2
-
1Have a look at [BackgroundWorker](http://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDUQFjAA&url=http%3A%2F%2Fmsdn.microsoft.com%2Fit-it%2Flibrary%2Fsystem.componentmodel.backgroundworker(v%3Dvs.95).aspx&ei=IVb1ULX0IoiZtQb-_YGIDQ&usg=AFQjCNFRVGcrEN3-rSleLQLwxJfbUgg1dA&bvm=bv.41018144,d.Yms), it has been created specifically for this purpose... – digEmAll Jan 15 '13 at 13:14
-
@Soner: Thanks, but I disagree. There are thread solutions fitted specific to WinForms ui controls. I think your title makes it too generic. (such as BeginInvoke). Re-edited. – Bick Jan 15 '13 at 13:16
4 Answers
6
In a first step start with
If this doesn't meet your requirements or you need more advanced stuff you should take a look into one of these:

Oliver
- 43,366
- 8
- 94
- 151
3
Invoke((MethodInvoker) delegate {
DoSomething();
});

Otiel
- 18,404
- 16
- 78
- 126
-
That sounds like the opposite way from a background thread to the UI thread. – Uwe Keim Jan 15 '13 at 13:20
-
@UweKeim: Not sure what you mean. This code can be used to call a method from a thread that's not the UI thread. Seems to me that it's what the OP asked. – Otiel Jan 15 '13 at 13:24
-
1
0
You should call Control.Invoke
or BeginInvoke
, see in-depth reference here.

Tommaso Belluzzo
- 23,232
- 8
- 74
- 98
-1
You can do like this
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
//DO SOMETHING
}

Alex
- 8,827
- 3
- 42
- 58