Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to await async tasks during a button click?

I have a refresh button in my app that uses some async methods to update the list of items displayed. The problem is that I can't have a return type of Task for the event handler for the button click so I'm left with an async void method. Thus, the user can hit the refresh button, then select an item while the list is being repopulated which will result in an error.

start of code that handles button click:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {


        await ViewModel.CreateMessageCommand();

So is there anyway to properly await for this task to finish?

like image 656
Orch Avatar asked Jul 22 '13 16:07

Orch


People also ask

What is the difference between task run and async await?

Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

Does async await stop execution?

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment.

What happens when you call async method without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Is async await same as synchronous?

Async/await helps you write synchronous-looking JavaScript code that works asynchronously. Await is in an async function to ensure that all promises that are returned in the function are synchronized. With async/await, there's no use of callbacks.


Video Answer


3 Answers

Since event handlers for controls typically return void, you need to handle this in a different manner. This often means, in a scenario like yours, that you need to disable all or part of your UI while things are loading, i.e.:

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    // Make the "list" disabled, so the user can't "select an item" and cause an error, etc
    DisableUI();

    try
    {    
       // Run your operation asynchronously
       await ViewModel.CreateMessageCommand();
    }
    finally
    {
       EnableUI(); // Re-enable everything after the above completes
    }
}
like image 153
Reed Copsey Avatar answered Oct 15 '22 13:10

Reed Copsey


You should simply disable all of the UI controls that the user shouldn't be interacting with at the start of the action, and then enable them at the end.

like image 41
Servy Avatar answered Oct 15 '22 11:10

Servy


One way would be to wrap the View in a BusyIndicator from the WPF Toolkit

You would provide a bool property on your viewmodel and toggle the value at the start and end.

It puts up a UI element above all the other controls preventing user interaction but providing an animated busy message, which can be updated to say whatever you wish.

like image 42
ywm Avatar answered Oct 15 '22 12:10

ywm