2014-09-11 71 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