Is there any guarantee that how a task created using the factory task will not be checked in the user interface thread in WPF?

I used to use background workflows to make service calls and did not block the user interface thread. Currently started using TPL, but ideally tasks should be used for tasks with heavy CPU usage.

So wondering if something is wrong with using task processing to make service calls, since there will be many threads that will sit idle until the service calls are completed?

And as for the original question, is there any guarantee that the created task will always be called in a thread other than the user interface thread in the WPF application?

+3
source share
2 answers

Tasks are abstractions for work that you plan to launch somehow. It does not have to be CPU intensive.

Usually, when you create it Task, it uses the default scheduler, which schedules it in the thread pool (which means that it will not be in the user interface thread). You can use it TaskScheduler.FromCurrentSynchronizationContextto create a scheduler that will send work to the user interface thread.

To perform behavior similar BackgroundWorkerto tasks, you can use:

var syncScheduler = TaskScheduler.FromCurrentSynchronizationContext(); // must be called on the UI thread
var task = ... // create the task
task.ContinueWith(t => { /* update the UI here */ }, syncScheduler);

, , -, WCF APM TaskFactory.FromAsync, .

+6

All Articles