如何维护C#进程?

时间:2015-07-03 11:06:15

标签: c#

我写了这个:

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        // I do many thinks with this _recognizer
    }
}

我的应用程序立即启动和停止。我应该怎么做才能使我的应用程序保持开放并响应我的_recognizer?我不想写一个Windows表单或控制台应用程序。我想将语音识别器作为后台应用程序。

4 个答案:

答案 0 :(得分:1)

这是一个控制台应用程序,我猜想..

Console.Readline();

将等待用户输入enter,然后退出。

更新

看看这个帖子Developing a simple Windows system tray desktop app to consume a .NET web service。 也许这与您的需求相似。

答案 1 :(得分:0)

当main方法返回时,应用程序将关闭,是的。在这种情况下,所有背景 - 线程都会中止。剩下的是前景线程。他们让这个过程保持活力。这就是消息循环的作用。

因此,您必须在前台线程中有任何类型的循环(在main方法或任何新创建的线程中IsBackground - 属性设置为false)。

这可能看起来像:

SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
while (_recognizer.IsActive) // or something similar
{
    Thread.Sleep(100);
}

这不是一种美,因为它浪费资源并使用Thread.Sleep。当SpeechRecognitionEngine有一个喜欢退出的事件时,您可以使用以下内容:

ManualResetEvent reset = new ManualResetEvent(false);
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
_recognizer.Quitting += new EventHandler((sender, args) =>
{
    reset.Set();
});
reset.WaitOne();

ManualResetEvent允许您等待Quitting - 事件。调用Set后,WaitOne返回,您的应用程序/流程结束。

答案 2 :(得分:0)

你应该写

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        _completed = new ManualResetEvent(false);
        SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
        // I do many thinks with this _recognizer
        // Add an exit event that should call _completed.Set();
        _completed.WaitOne();
    }
}
找到了here

的实施和教程
static void Main(string[] args)
{
    _completed = new ManualResetEvent(false);
    SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
    _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) Name = { "testGrammar" }); // load a grammar
    _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("exit")) Name = { "exitGrammar" }); // load a "exit" grammar
    _recognizer.SpeechRecognized += _recognizer_SpeechRecognized;
    _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device
    _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous
    _completed.WaitOne(); // wait until speech recognition is completed
    _recognizer.Dispose(); // dispose the speech recognition engine
}

void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
     if (e.Result.Text == "test") // e.Result.Text contains the recognized text
     {
         Console.WriteLine("The test was successful!");
     } 
     else if (e.Result.Text == "exit")
     {
         _completed.Set();
     }
}

答案 3 :(得分:-3)

也许您想要运行Windows服务?你可以查看它here

protected override void OnStart(string[] args)
{ 
    eventLog1.WriteEntry("my service started"); 
}

教程的一部分您可以在里面插入SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();代码。我认为会听一些事件。您也可以在此处设置这些事件的回调。

当您完全关闭应用程序时,请在此处理_recognizer:

protected override void OnStop()
{ 
    eventLog1.WriteEntry("my service stoped");
}