Update UI before running the rest of method

不羁的心 提交于 2019-12-05 04:45:59

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.

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
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!