OK, I've duplicated the problem in a little bit of code. I've put it below,
you must excuse any line-wrap.
If you want to see, create a console project.Place the code I have below the
Project.CPP file (the one with main()).
Be sure to add System.Speech as a reference, and set it up tp target .NET
Framework 3.5. This code will also likely only work on a Windows VISTA
machine (SAPI 5.3).
Describing the code:
In a 'globals' class it creates a Timer and a SpeechRecognitionEngine (that
recognizes 'yes' or 'no'). Say 'yes' or 'no' into the microphone, and he
Engine's Completed handler writes 'enable timer' to the console and enables
the timer. The Timer should after that write 'tick!' to the display 4 times
a second after that.
What happens is that you will see the 'enable timer' message, proving the
Completed handler fired and enabled Timer, but the 'Tick's never show up
indicating the Timer never fires!
Thus, whether a restriction or a bug, a Timer can't be enabled successfully
in any Speech Recognition event handler (this happens with all of them, I'm
just demonstrting Completed here).
Here is the code:
//------------------------
#include "stdafx.h"
#using <mscorlib.dll>
#using <System.DLL>
#using <System.Windows.Forms.DLL>
using namespace System::Windows::Forms ;
using namespace System ;
using namespace System::Speech::Recognition ;
typedef EventHandler<RecognizeCompletedEventArgs^Speech_Co mpleted_Handler
;
typedef System::Windows::Forms::Timer System_Timer ;
ref class globals
{
public:
static globals()
{
Timer = gcnew System::Windows::Forms::Timer() ;
Timer->Tick += gcnew EventHandler(&globals::Tick_Handler ) ;
Timer->Interval = 100 ;
Engine = gcnew SpeechRecognitionEngine() ;
Engine->SetInputToDefaultAudioDevice() ;
Engine->RecognizeCompleted += gcnew Speech_Completed_Handler(
&globals::Completed_Handler ) ;
Choices^ choices = gcnew Choices ;
choices->Add( "yes" ) ;
choices->Add("no" ) ;
Grammar^ grammar = gcnew Grammar(choices) ;
Engine->LoadGrammar(grammar) ;
}
static void Completed_Handler(Object^,RecognizeCompletedEventA rgs^)
{
Console::WriteLine( "enable timer" ) ;
Timer->Enabled = true ;
}
static void Tick_Handler( System::Object^,EventArgs^ )
{
Console::WriteLine( "tick!" ) ;
}
static SpeechRecognitionEngine^ Engine ;
static System::Windows::Forms::Timer^ Timer ;
} ;
int main(array<System::String ^^args)
{
Console::WriteLine("Speak!");
globals::Engine->RecognizeAsync() ;
Form^ form = gcnew Form() ;
Application::Run( form ) ;
return 0;
}
//------------------------
[==Peter==]
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:e%***************@TK2MSFTNGP02.phx.gbl...
>
"Peter Oliphant" <pe********@hotmail.comwrote in message
news:6F**********************************@microsof t.com...
>Note that although this involves SAPI, it is more a question about Timers
and event handlers.
I wrote a Speech Recognize handler (SAPI), and put some code in it to
enable a Timer. It would not do it. If I bring this same code outside
this event handler, it works just fine.
Is this normal?
Which "Timer"? .NET has at least three different classes named Timer.