locked
Does Async await in C# gives performance benefit? RRS feed

  • Question

  • I have tried using async await in wpf application for making an async wcf method call. I have a screen with multiple drop downs which are to be loaded with some reference data on screen load. All these drop downs get data from wcf so i have used async for these wcf calls. Can you please tell me if this will be beneficial & in what way?

    Minni

    Thursday, August 15, 2013 10:39 AM

Answers

All replies

  • Asynchronous programming improves the responsiveness of your application and asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web or a WCF service located on a different machine.

    If such an activity is executed within a synchronous process on the UI thread in a GUI application, such as a WPF or Windows Forms application, the entire application must wait and the user won't be able to do anything meaningful during the time of execution of the activity.

    For more information about asynchronous programming with the async and await keywords, please refer to the following article on MSDN: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

    Thursday, August 15, 2013 11:01 AM
  • Thanks for your reply. I understand that benefit of asynchrony but in my scenario, few points:-

    1. Its a windows app so we dont want user to perform another activities in between of one ongoing activity like user clicks on Save then we need to show Success message in end so we dont want user to do some other work in between.
    2. In above screen loading scenario, user will have to wait till the time all required drop downs gets loaded on screen so my question is in regards to these situations, whether i'll get some performance benefit or not.
    3. Also, i read async await will not use another thread, they'll run on main UI thread only so thats why i want to confirm if i'll have some benefit in terms of performance in above scenarios or not.

    Minni

    Thursday, August 15, 2013 11:53 AM
  • Asynchronicity is not a performance optimization for an individual operation, i.e. a synchronous method will not run any faster by making it asynchronous as it still needs to accomplish everything that the synchronous operation did, but with the additional constraints and considerations associated with asynchronicity.
    Thursday, August 15, 2013 12:14 PM
  • You can still lock the screen while you wait for the tasks to return.  As Magnus says, making your tasks asynchronous will not improve the speed of your code; however, it may improve the speed of your app.  For example, if you have to poll three different services to populate the combo boxes, then doing when asynchronously with a .WhenAll will allow the three calls to run at the same time.

    Regarding the threading: if I understand it correctly, whether or not a new thread is used is abstracted away from the program.  That is, you just say that you want to do something, and the CLR works out whether it would be best to start a new thread.


     
    Thursday, August 15, 2013 12:22 PM
  • So, i shouldnt make those 2-3 wcf service calls asynchronous for filling drop downs? I think i'll have some benefit if all 3 calls will run in parallel but i am not sure whether 3 service calls will run in parallel or not.

    Minni

    Thursday, August 15, 2013 3:35 PM
  • Using async await mechanism.

    Minni

    Thursday, August 15, 2013 3:36 PM
  • You can separate creating the task from awaiting the task if your program has other work to accomplish that doesn’t depend on the completion of the task. Between starting a task and awaiting it, you can start other tasks and these additional tasks will implicitly run in parallel as described on MSDN here: http://msdn.microsoft.com/en-us/library/vstudio/hh696703.aspx

    Also, the TPL has a Parallel.Invoke method which can be used to parallelize operations: http://msdn.microsoft.com/en-us/library/dd460705.aspx

    Thursday, August 15, 2013 3:50 PM
  • As I understand it, using async / await will do just that.  You'll need to structure the code so that it makes all three calls, and then wait for all to complete.  The calls will then be made at more or less the same time, and will return as and when they are ready.

    I believe this is not the same as parallel processing, as that would involve all the calls being made in parallel (which you don't need).

    Friday, August 16, 2013 12:36 PM
  • Why do you think that i dont need 3 calls in parallel? My understanding is starting 3 service calls in parallel will help in getting data faster than making 3 calls synchronously. So, i just want to understand that async/await usage in service call will help in this or not without using old ways including TPL or backgroundworker etc.

    Minni

    Monday, August 19, 2013 3:01 PM