问题
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?
回答1:
In a first step start with
- BackgroundWorker
If this doesn't meet your requirements or you need more advanced stuff you should take a look into one of these:
- Task Parallel Library
- ReactiveExtensions
- async / await
回答2:
Invoke((MethodInvoker) delegate {
DoSomething();
});
回答3:
You should call Control.Invoke
or BeginInvoke
, see in-depth reference here.
回答4:
You can do like this
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
//DO SOMETHING
}
来源:https://stackoverflow.com/questions/14338504/winforms-invoking-a-method-in-another-thread