【问题标题】:C# WinForm not Responsive - System.Speech - HelpC# WinForm 无响应 - System.Speech - 帮助
【发布时间】:2010-02-17 18:01:18
【问题描述】:

这是来自 C# Windows 窗体的代码

SpeechSynthesizer audio = new SpeechSynthesizer(); 
audio.Speak(textBox1.Text);
  • 这将读取文本框中的任何内容

尝试实现暂停和停止功能时出现问题。当代码读取某些内容时,任何按钮或菜单项都不会被点击

public void button1_Click(object sender, EventArgs e)
    {
        //Nothing gets executed here when the code is reading
    }

我刚刚读到有 SpeakProgressEventArgs http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speakprogresseventargs%28VS.85%29.aspx

我尝试了 synth...asyncancel...但是按钮的点击事件没有被执行

【问题讨论】:

  • SpeechSynthesizer 是否在 UI 线程中运行?如果它与 UI 事件循环在同一线程中运行,您的 UI 响应可能会很差。

标签: c# speechsynthesizer


【解决方案1】:

请改用 SpeakAsync() 方法。这可以防止 UI 阻塞 Speak() 方法,它在被阻塞时无法响应按钮单击。您可以使用 SpeakAsyncCancelAll() 来阻止它继续喋喋不休。

【讨论】:

    【解决方案2】:

    你需要使用Threads管理这个块audio.Speak(textBox1.Text);

            Thread t = new Thread(() =>
            {
                SpeechSynthesizer audio = new SpeechSynthesizer(); 
                audio.Speak(textBox1.Text);
            });
            t.Start();
    

    现在如何停止正在运行的线程?这张海报很好地解释了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2012-08-06
      • 2011-06-10
      • 2021-03-30
      • 2011-06-14
      • 1970-01-01
      相关资源
      最近更新 更多