0

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..

8
  • 1
    Why do you want to do that? Purpose of the async methods are to work asynchronously. Commented Sep 11, 2014 at 6:35
  • I am using Text to speech, and then showing some call dialog, and when i show call dialog app crashes, because this dialog breaks speech because its async.. thats why i would like to wait for speech to complete and then run call dialog.. Commented Sep 11, 2014 at 6:37
  • Oh ok. Are you using SpeechSynthesizer? Commented Sep 11, 2014 at 6:41
  • yes, I am using SpeechSynthesizer Commented Sep 11, 2014 at 6:43
  • Ok see my answer below Commented Sep 11, 2014 at 6:49

1 Answer 1

1

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"); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I am actually doing this.. SpeechSynthesizer synth = new SpeechSynthesizer(); await synth.SpeakTextAsync(text); and after that I call phoneCallTask.Show() and dialog pops up, and when I close it, exception is thrown and it says that speech has been disrupted by a phone call...
I have added boolean at the end of speak method, and now speech is executed before call dialog.. but however, when i call dialog is closed, and i want to go back to speech, app crashes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.