简体   繁体   中英

when to use await key word? do I need it in this case?

i have a onClick method on a buttom. when this button is pressed, i get a displayalert message.

getTokenQ has a value. my question why its going inside catch block?

    private async void Button_Clicked(object sender, EventArgs e)
    {
        try
        {
            var getTokenQ = await SecureStorage.GetAsync("Save_Security_Question");

            if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
            {
            }
            else
            {
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
        }
    }

no you don't need to do that, you can use .Result if you don't want to execute your code asynchronously , this code is perfectly fine :

private void Button_Clicked(object sender, EventArgs e)
    {
        try
        {
            var getTokenQ = SecureStorage.GetAsync("Save_Security_Question").Result;

            if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
            {
            }
            else
            {
            }
        }
        catch (Exception ex)
        {
            DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
        }
    }

Why in catch ! Because it's a message to display only if an exception is thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM