开发者

What is recommended way to perform async tasks in WPF?

Are there any standard tools, or recommended approaches for async tasks ex开发者_Go百科ecution?

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.


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.


Have a look at below post, it describes a way to create an async delegate command(using BackgroundWorker). I have used this kind of command in our application and it works fine and at the same time it provides a consistent way of doing things asynchronously.

An Asynchronous Delegate Command for your WPF MVVM Apps - AsyncDelegateCommand http://amazedsaint.blogspot.com/2010/10/asynchronous-delegate-command-for-your.html

A similar implementation is also mentioned here - Asynchronous WPF Commands


depends what you are trying to do async. e.g. calling a WCF service I'd use the build-in way, with the Completed pattern that does the marshalling for you. Normal Background work I'd use the BackgroundWorker as you again don't need to worry about the marshalling.


In addition to standard threads. One thing to use are Async methods of many classes, that can do it. This includes web service requests, file read/write operations.

One thing to look at is Coroutines, used mainly by Caliburn.Micro . But its not standard way to do it.

Also .NET 4 adds Task class along with ParallelExtensions, that is capable to do some async programming easier. But its still clumsy, so .NET 5 adds async programing model, to make thigs even easier. But god knows when its going to be released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜