Best practices to use await-async, where to start the task? Best practices to use await-async, where to start the task? wpf wpf

Best practices to use await-async, where to start the task?


Which option to choose?

I would use neither of your options, both of them will create an misleading API, everyone that will use your service will think that he uses async methods but the truth is that behind the false signature the methods are actually not asynchronous at all.
Your service just pushes the work to another ThreadPool thread that will be blocked during the method execution.

While in the client side that does not sound that bad in server side using this principle can really hurt your scalability.

According to Stephen Cleary:

do not use Task.Run in the implementation of the method; instead, use Task.Run to call the method.

You shouldn't wrap your service methods with fake async signatures if the methods are really synchronous, if you don't want to block the UI thread while the heavy method is executing you should use Task.Run when you are calling the services methods from the view model.

I suggest you to read Stephen Cleary series of Task.Run Etiquette articles in his blog.

Consider using async methods for I/O operations

Moreover I can see that the work your service does is not only CPU bound work, if so you should consider to use built in I/O async API method if there is any available over the synchronous you use now (for example Asynchronous File I/O), in this case your methods will be a true async methods and not a fake async wrappers as they are now.

If you will do it your UI thread will not block while the I/O async operation will execute, but if there is still also heavy CPU bound work involved and you don't want to block the UI during the CPU bound work execution you can still use Task.Run when you call the service method from the view model (even though the method signature is already an async one).

More about mixture of synchronous and asynchronous methods in the series of articles above.

Another great advantage of using bult in async API methods is that if the method is truly async you do not block any of the ThreadPool threads while the async I/O operation is executing and more ThreadPool threads are free to do any other work.
This is especially (but not only) important while using async programming in the server side and it can really boost your scalability.

Async and MVVM

One last thing, if you are following the MVVM pattern MSDN "async MVVM" articles are a great reading material you can use.