简体   繁体   中英

Windows phone 8 make async method behave like its sync

I am new to async programming and I am wondering if you can fake c# async methods to make it work like its sync? Or if you can make it wait for it to complete before executing another method?

In my case :

await Speak("Do you want me to call 123 ?");
   if (isComplete)
       {
          PhoneCallTask phone = new PhoneCallTask();
           phone.PhoneNumber = "123";
            phone.Show();

        } 
await Speak("blabla");

isComplete is global boolean..

here is Speak method:

private async Task Speak(string text)
    {
    SpeechSynthesizer synth = new SpeechSynthesizer();
    await synth.SpeakTextAsync(text);
    isComplete = true;
    }

It says first text, than shows dialog.. after dialog is cloes it crashes..

You can use await keyword

See following example taken from MSDN

// Declare the SpeechSynthesizer object at the class level.
SpeechSynthesizer synth;

// Handle the button click event.
private async void SpeakFrench_Click_1(object sender, RoutedEventArgs e)
{
  // Initialize the SpeechSynthesizer object.
  synth = new SpeechSynthesizer();

  // Query for a voice that speaks French.
  IEnumerable<VoiceInformation> frenchVoices = from voice in InstalledVoices.All
                     where voice.Language == "fr-FR"
                     select voice;

  // Set the voice as identified by the query.
  synth.SetVoice(frenchVoices.ElementAt(0));

  // Count in French.
  await synth.SpeakTextAsync("un, deux, trois, quatre");
}

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