locked
How to wait for async method before executing the next line

    Question

  • that await method contains speech recognition compile constraint async

    private async void startStopButton_Click(object sender, RoutedEventArgs e) // i cant make the returntype to task { colorRandomizer.changeColor(colorList.ColorBrush); await speechRecognition.ListenForVoice(); // how can I wait for this. textToChange.Text = "nothing"; // before this happens textToChange.Foreground = colorRandomizer.SolidColorBrush; // }

    Thursday, March 12, 2015 4:52 AM

Answers

  • Hi,

    Move the awaitable code to a separate method inside a task and then use await on that method.

            private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
            {
                await RunAsyncMethod();
            }

            private Task RunAsyncMethod()
            {
                Task.Delay(5000);
                return Task.FromResult(true);
            }


    Please mark this as answer if you find it useful.
    Thursday, March 12, 2015 9:53 AM
  • is speechRecognition.ListenForVoice returnning a waitable Task?

    if not you need convert it to one,

    Here is some snippet to illustrate how to do it

    void Task<bool> ListenForVoiceAsync()
    {
       return Task<bool>.RunAsync(()=>
       {
         speechRecognition.ListenForVoice();
         speechRecognition.WaitUntilThereIsSomethingHappening(); //wait for a condition
         return true;
       });
    }

    then you may

    bool isSuccess=await this.ListenForVoiceAsync();



    Friday, March 13, 2015 2:32 AM
  • This method is correct, but only if speechRecognition.ListenForVoice() returns a Task.

    What code do you have in the ListenForVoice() method? It should resemble this:

    public async Task ListenForVoice()
    {
      // don't complete until voice has been listened to
      // when this method completes the program will revert control back to the button click handler
    
    }

    It's OK to use async void on a button click handler

    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Friday, March 13, 2015 6:09 AM

All replies

  • Hi,

    Move the awaitable code to a separate method inside a task and then use await on that method.

            private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
            {
                await RunAsyncMethod();
            }

            private Task RunAsyncMethod()
            {
                Task.Delay(5000);
                return Task.FromResult(true);
            }


    Please mark this as answer if you find it useful.
    Thursday, March 12, 2015 9:53 AM
  • is speechRecognition.ListenForVoice returnning a waitable Task?

    if not you need convert it to one,

    Here is some snippet to illustrate how to do it

    void Task<bool> ListenForVoiceAsync()
    {
       return Task<bool>.RunAsync(()=>
       {
         speechRecognition.ListenForVoice();
         speechRecognition.WaitUntilThereIsSomethingHappening(); //wait for a condition
         return true;
       });
    }

    then you may

    bool isSuccess=await this.ListenForVoiceAsync();



    Friday, March 13, 2015 2:32 AM
  • This method is correct, but only if speechRecognition.ListenForVoice() returns a Task.

    What code do you have in the ListenForVoice() method? It should resemble this:

    public async Task ListenForVoice()
    {
      // don't complete until voice has been listened to
      // when this method completes the program will revert control back to the button click handler
    
    }

    It's OK to use async void on a button click handler

    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Friday, March 13, 2015 6:09 AM