2016-11-03 101 views
0

下面的代碼,其中timer_tick事件檢查條件匹配以觸發文本到語音和音頻文件的播放。但是當條件滿足時,結果如預期得到,但程序凍結。Timer_Tick凍結程序

此外如何在這些條件匹配時添加彈出消息框。可以重複播放音頻文件或語音,但我不想在計時器滴答時重複打開消息框。

private void timer1_Tick(object sender, EventArgs e)  
{ 
    label_time1.Text = DateTime.Now.ToString("hh:mm:ss tt"); 
    label6.Text = DateTime.Now.ToShortDateString(); 
    if (((DateTime.Now.ToString("hh:mm tt") == (textBox_ordertostart.Text) && (DateTime.Now.ToShortDateString() == dateTimePicker2.Value.ToShortDateString()) && (checkBox_delivery.CheckState == 0) && (checkBox_pickup.CheckState != 0)))) 
    { 
     comboBox_suburb.Enabled = false; 
     checkBox_delivery.Enabled = false; 
     mplayer = new SoundPlayer(WindowsFormsApplication17.Properties.Resources.good_morning); 
     mplayer.Play(); 


     pbuilder.ClearContent(); 

     pbuilder.AppendText("Pickup order time start now"); 


     txt2speech.Speak(pbuilder); 
     pbuilder.AppendBreak(PromptBreak.Medium); 
    } 
+0

您使用[SpeechSynthesizer類](https://msdn.microsoft.com/en-us/library/office/system.speech.synthesis.speechsynthesizer.aspx)嗎?如果是這樣,請嘗試'SpeakAsync'而不是'Speak'。 – adv12

+0

太好了。這很好,謝謝。任何關於消息框的想法? –

回答

0

當使用Speak功能時,SpeechSynthesizer將保存程序。嘗試使用SpeakAsync()而不是Speak(),或者如果不希望tick中的函數阻止程序的UI,則可以啓動一個運行tick的新線程。