怀疑c#处理程序?

时间:2010-05-20 20:55:43

标签: c# event-handling

我在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是一种语音识别方法,用于加载语法并在此处生成识别处理程序:

recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);

现在我遇到了一个问题,当我在方法运行之前调用方法startRecognition时,两个处理程序(识别一个和Tick的处理程序)都运行良好。如果识别出一个单词,则调用handlerRecognition方法。

但是,当我在方法startRecognition之前调用方法运行时,两个方法似乎都运行良好但是然后识别处理程序永远不会被执行!即使我看到单词被识别(因为它们发生在Windows语音识别应用程序上)。

我还能为识别处理程序做些什么呢?

1 个答案:

答案 0 :(得分:0)

如果在运行之前没有调用StartRecognition,那么处理程序永远不会被连接起来。因此,显而易见的解决方案是确保连接识别处理程序的代码行被调用。

最简单的解决方案是最好的解决方案:)

我在run()方法中创建了识别处理程序。

我在运行后调用startReconition方法并且它有效!

感谢Robert Harvey