Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is recommended way to perform async tasks in WPF?

Are there any standard tools, or recommended approaches for async tasks execution?

UPD I understand, how to use threads. I only need to know the recommended WPF way to block UI while performing async call, and how to update progress info.

like image 305
user536232 Avatar asked Feb 18 '11 07:02

user536232


People also ask

Which is the recommended way to wait for an async method to complete?

GetAwaiter. GetResult pattern to actually convert async to sync. But if you do that byitself in WPF or another SynchronizationContext bound to a particular thread you deadlock. This code avoids deadlock by transferring the async operation to the threadpool which is not synchronized to a particular thread.

How do I use async tasks?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

Why you shouldn't use async void?

Async void methods can wreak havoc if the caller isn't expecting them to be async. When the return type is Task, the caller knows it's dealing with a future operation; when the return type is void, the caller might assume the method is complete by the time it returns.

What are some background processes that need to be performed using async?

An AsyncTask streamlines the following common background process: Pre - Execute code on the UI thread before starting a task (e.g show ProgressBar) Task - Run a background task on a thread given certain inputs (e.g fetch data) Updates - Display progress updates during the task (optional)


1 Answers

You can use several ways, for example:

  • Thread pool
  • Background Worker
  • Plain old threads

And since .NET 4, the preferred way is to use Tasks.

like image 97
Daniel Hilgarth Avatar answered Oct 31 '22 03:10

Daniel Hilgarth