Doubt with c# handlers?

Posted by aF on Stack Overflow See other posts from Stack Overflow or by aF
Published on 2010-05-20T20:55:42Z Indexed on 2010/05/20 21:30 UTC
Read the original article Hit count: 241

Filed under:
|

I have this code in c#

public void startRecognition(string pName)
    {
        presentationName = pName;

        if (WaveNative.waveInGetNumDevs() > 0)
        {
            string grammar = System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Presentations\\" + presentationName + "\\SpeechRecognition\\soundlog.cfg";

/*                if (File.Exists(grammar))
                {
                    File.Delete(grammar);
                }
                executeCommand();*/
            recContext = new SpSharedRecoContextClass();
            recContext.CreateGrammar(0, out recGrammar);

            if (File.Exists(grammar))
            {
                recGrammar.LoadCmdFromFile(grammar, SPLOADOPTIONS.SPLO_STATIC);
                recGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
                recGrammar.SetRuleIdState(0, SPRULESTATE.SPRS_ACTIVE);
            }

            recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);
            //recContext.RecognitionForOtherContext += new _ISpeechRecoContextEvents_RecognitionForOtherContextEventHandler(handleRecognition);

            //System.Windows.Forms.MessageBox.Show("olari");


        }
    }

    private void handleRecognition(int StreamNumber,
        object StreamPosition,
        SpeechLib.SpeechRecognitionType RecognitionType,
        SpeechLib.ISpeechRecoResult Result)
    {
        System.Windows.Forms.MessageBox.Show("entrei");

        string temp = Result.PhraseInfo.GetText(0, -1, true);
        _recognizedText = "";

        foreach (string word in recognizedWords)
        {
            if (temp.Contains(word))
            {
                _recognizedText = word;
            }
        }
    }





public void run()
    {
        if (File.Exists(System.Environment.GetEnvironmentVariable("PUBLIC") + 

"\\SoundLog\\Serialization\\Voices\\identifiedVoicesDLL.txt"))
    {
        deserializer = new XmlSerializer(_identifiedVoices.GetType());
        FileStream fs = new FileStream(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\identifiedVoicesDLL.txt", FileMode.Open);
        Object o = deserializer.Deserialize(fs);
        fs.Close();
        _identifiedVoices = (double[])o;
    }


    if (File.Exists(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\deletedVoicesDLL.txt"))
    {
        deserializer = new XmlSerializer(_deletedVoices.GetType());
        FileStream fs = new FileStream(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\deletedVoicesDLL.txt", FileMode.Open);
        Object o = deserializer.Deserialize(fs);
        fs.Close();
        _deletedVoices = (ArrayList)o;
    }

    myTimer.Interval = 5000;
    myTimer.Tick += new EventHandler(clearData);
    myTimer.Start();
    if (WaveNative.waveInGetNumDevs() > 0) {
        _waveFormat = new WaveFormat(_samples, 16, 2);
        _recorder = new WaveInRecorder(-1, _waveFormat, 8192 * 2, 3, new BufferDoneEventHandler(DataArrived));
        _scaleHz = (double)_samples / _fftLength;
        _limit = (int)((double)_limitVoice / _scaleHz);
        SoundLogDLL.MelFrequencyCepstrumCoefficients.calculateFrequencies(_samples, _fftLength);
    }
}

startRecognition is a method for Speech Recognition that load a grammar and makes the recognition handler here:

recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);

Now I have a problem, when I call the method startRecognition before method run, both handlers (the recognition one and the handler for the Tick) work well. If a word is recognized, handlerRecognition method is called.

But, when I call the method run before the method startRecognition, both methods seem to run well but then the recognition Handler is never executed! Even when I see that words are recognized (because they happear on the Windows Speech Recognition app).

What can I do for the recognition handler be allways called?

© Stack Overflow or respective owner

Related posts about c#

Related posts about event-handling