0

I wrote this:

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

My applications starts and stops immediately. What should I do to so that my application remains open and responsive to my _recognizer ? I don't want to write a Windows form or console application. I would like to keep my voice recognizer as background application.

Veer
  • 1,575
  • 3
  • 16
  • 40
  • What "thinks" do you do with ``_recognizer``? Please show us your full code – Binkan Salaryman Jul 03 '15 at 11:10
  • I implement the grammar and then send action on recognized words. That may be starting an application or responding with text to speech. –  Jul 03 '15 at 11:12
  • 3
    The code is not helping for this question. –  Jul 03 '15 at 11:14
  • 1
    @m4tth132kshsdvg You want a [Windows Service](https://en.m.wikipedia.org/wiki/Windows_service)? – Yuval Itzchakov Jul 03 '15 at 11:17
  • No. A simple background application. I don't need a console, I don't need a windows. It can be a service but I don't need to start it as a service. I just need an application that work in background. Actually the user interface is only controlled and received by speech. –  Jul 03 '15 at 12:08
  • Thus, you need a loop maintaining a thread open. – Patrik Jul 03 '15 at 12:58

4 Answers4

1

Is this a Console Application, I do suppose..

Console.Readline();

will wait user to type enter, and then exit.


UPDATE

Look at this thread Developing a simple Windows system tray desktop app to consume a .NET web service. Maybe this is similar to your needs.

Community
  • 1
  • 1
Emanuele Greco
  • 12,551
  • 7
  • 51
  • 70
  • It's ``Console.ReadKey(true);`` – Binkan Salaryman Jul 03 '15 at 11:08
  • 1
    I just added a precision in my question: I don't want to write a Windows form or console application. I would like to keep my voice recognizer as background application. –  Jul 03 '15 at 11:09
  • Can't tell anything about that without the implementation details of ``SpeechRecognitionEngine``... – Binkan Salaryman Jul 03 '15 at 11:11
  • the working bit of the application needs to be in some form of loop which responds to the Windows messages & the Speech recognition engine. – PaulF Jul 03 '15 at 11:13
0

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.

Patrik
  • 1,355
  • 12
  • 22
0

You should write

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();
    }
}

implementation and tutorial found 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();
     }
}
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
-3

Maybe You want a Windows Service to run? You can check out it here.

In the

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

part of the tutorial You can insert the SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine(); code inside. I assume that will listen to some events. You set the callbacks for those events also here.

When You fully close the application You take care of the _recognizer here:

protected override void OnStop()
{ 
    eventLog1.WriteEntry("my service stoped");
}
ntohl
  • 2,067
  • 1
  • 28
  • 32
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Wtower Jul 03 '15 at 12:12
  • I have edited the comment, but the link's step by step tutorial is pretty covers everything. – ntohl Jul 03 '15 at 12:30
  • 1
    @Wtower Yes, but what the user is asking for is literally that. Even if this specific page gets removed he can just ask Google and pick the first search result. On the contrary, how a Windows Service Needs to be set up in VS is not easy to Show here in an answer-format without a couple of screenshots or detailed explanations. Also if we just copy/paste the tutorial here the original poster on codeproject will surely be upset... I am disappointed that my answer got downvoted although it's exactly what he has been asking for, while some other nonsense with Console.ReadKey has gotten an upvote. – KarmaEDV Jul 03 '15 at 12:41
  • @KarmaEDV sorry that your answer got downvoted. If it is any comfort I have not downvoted any of these two answers, but I can understand that other users may have downvoted them because any link is required to have a context in SO answers. It is not enough that anyone can "just google" it. http://stackoverflow.com/help/how-to-answer – Wtower Jul 03 '15 at 12:46