2014-09-11 69 views
0

我是新來的異步編程,我想知道如果你可以假冒c#異步方法,使其工作像它的同步?或者如果你可以讓它在執行另一種方法之前等待它完成?Windows Phone 8使異步方法的行爲像它的同步

在我的情況:

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

     } 
await Speak("blabla"); 

isComplete是全球性的布爾..

這裏是講方法:

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

它說第一個文本,不是顯示對話框..後對話框它的墜毀暈..

+1

爲什麼你想這樣做多久?異步方法的目的是異步工作。 – Sam 2014-09-11 06:35:37

+0

我正在使用文本到語音,然後顯示一些調用對話框,當我顯示調用對話框應用程序崩潰時,因爲此對話因爲它的異步而打破了語音..那就是爲什麼我想等待語音完成,然後運行調用對話框.. – n32303 2014-09-11 06:37:29

+0

噢好吧。你在使用SpeechSynthesizer嗎? – Sam 2014-09-11 06:41:41

回答

1

您可以使用await關鍵字

參見下面的示例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"); 
} 
+0

我實際上在做這個.. SpeechSynthesizer synth = new SpeechSynthesizer(); await synth.SpeakTextAsync(text); 之後,我打電話phoneCallTask​​.Show()和彈出對話框,當我關閉它,拋出異常,它說,講話已被打擾的電話...... – n32303 2014-09-11 06:49:06

+0

我已經添加了布爾結束說話方法,現在語音在對話前執行..但是,當我打電話對話結束時,我想回到講話,應用程序崩潰 – n32303 2014-09-11 06:52:13

+0

好的,你可以發佈你的代碼? – Sam 2014-09-11 06:55:08