When the main-method returns, the application gets closed, yes. In that event, all background-threads are aborted. What remains are foreground-threads. They keep the process alive. That's what the Message Loop does.
So, you must have any kind of loop in a foreground-thread (either in the main-method or any newly created thread with the IsBackground
-property set to false).
That could look like:
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
while (_recognizer.IsActive) // or something similar
{
Thread.Sleep(100);
}
That is fairly not a beauty, since it wastes resources and makes usage of Thread.Sleep
. When SpeechRecognitionEngine
has an event when it likes to quit, you could have something like:
ManualResetEvent reset = new ManualResetEvent(false);
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
_recognizer.Quitting += new EventHandler((sender, args) =>
{
reset.Set();
});
reset.WaitOne();
The ManualResetEvent
allows you to wait for the Quitting
-event. After Set
was called, WaitOne
returns and your application/process ends.