What is recommended way to perform async tasks in WPF?

后端 未结 4 1363
心在旅途
心在旅途 2021-01-18 12:52

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

相关标签:
4条回答
  • 2021-01-18 13:30

    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

    0 讨论(0)
  • 2021-01-18 13:42

    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.

    0 讨论(0)
  • 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 讨论(0)
  • 2021-01-18 13:52

    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.

    0 讨论(0)
提交回复
热议问题