问题
I have a long-running operation that have to be done in UI thread (involves UI elements that cannot be freezed). I want to display a busy indicator before running the operation.
busyIndicator.Visibility = Visibility.Visible;
LongRunningMethod();
busyIndicator.Visibility = Visibility.Collapsed;
Of course this does not work because rendering does not occur until the operation finishes. I tried to use Task.Yield() to run the rest of method asynchronously:
busyIndicator.Visibility = Visibility.Visible;
await Task.Yield();
LongRunningMethod();
This also does not work, as far as I understand, because the rest of the method is prioritized higher than rendering operation.
How can I do it using TPL?
UPD: LongRunningMethod cannot be run in a separate thread by its nature (works with complex WPF 3D models), and anyway I cannot afford to make changes in it now. So please don't offer solutions based on running it completely or partially on a separate thread.
回答1:
If you want to break UI method execution, then you have to use async/await.
Something like (untested)
busyIndicator.Visibility = Visibility.Visible;
await Task.Run(() => await Task.Delay(1)); // here method will exit and repaint will occurs
LongRunningMethod();
busyIndicator.Visibility = Visibility.Collapsed;
But depending on how long method runs, you may want to put it completely into another thread (Task, BackgroundWorker) and only invoke methods when you need to be in UI thread.
回答2:
This is quite trivial when you learn how to async and await work. You can read about that on msdn
Your problem is that you are never starting any asynchronous work - Task.Yield is already called from the UI context and will do nothing.
Try this:
async Task MyProcess() //the Task is returned implicitly
{
busyIndicator.Visibility = Visibility.Visible;
await LongRunningMethod(); //the work here is done in a worker thread
busyIndicator.Visibility = Visibility.Collapsed; //this line executes back on the ui context
}
Task LongRunningMethod() //the Async suffix is a convention to differentiate overloads that return Tasks
{
var result1 = await Task.Run(() => /* do some processing */ ); //we are actually starting an asynchronous task here.
//update some ui elements here
var result2 = await Task.Run(() => /* do some more processing */ );
//update some ui elements here
}
回答3:
Assuming your long running method is not async you should be able to call it inside Task.Run and await the result, or even better use TaskFactory.StartNew and pass in TaskCreationOptions.LongRunning.
busyIndicator.Visibility = Visibility.Visible;
await TaskFactory.StartNew(() => LongRunningMethod(),TaskCreationOptions.LongRunning);
busyIndicator.Visibility = Visibility.Collapsed;
On a side note, you need to be careful with Task.Yield because in some scenarios such as with WindowsForms the task scheduled on the SynchronizationContext has a higher priority than repainting the window so you can make your UI non-responsive if you repeatedly call Task.Yield, such as in a while loop.
来源:https://stackoverflow.com/questions/27418202/update-ui-before-running-the-rest-of-method