Home » U++ Library support » U++ Callbacks and Timers » How to set a TimeCallback
How to set a TimeCallback [message #50667] |
Sun, 02 December 2018 00:48  |
 |
forlano
Messages: 1207 Registered: March 2006 Location: Italy
|
Senior Contributor |
|
|
Hello,
after reading many posts on the forum I am not able to realize what I want.
After pressing button btnStart the program begin to read a sequence of words. This is done via the SAPI library (text to speech). However I want permit the user to stop the reading at any time, button btnStop, even before the end of the sequence. To do it I used a SetTimeCallback linked to the callback Play. This is one of the version I used:
void VegaTab3::Start()
{ btnStart.Disable();
btnStop.Enable();
int int_row = arrp.GetCursor();
if (int_row < 0) return; //prevent the crash of the application when int_row = -1)
for (int i = int_row; i<arrp.GetCount(); i++) {
SetTimeCallback(500, THISBACK1(Play, i));
}
btnStart.Enable();
btnStop.Disable();
}
void VegaTab3::Play(int i)
{
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL))) return ;
//std::wstring in = L"";
WString in;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if(SUCCEEDED(hr))
{
//...
// select a word
// ...
hr = pVoice->Speak((LPCWSTR)in, NULL, NULL); //play it
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
}
void VegaTab3::Stop()
{
KillTimeCallback();
btnStart.Enable();
btnStop.Disable();
}
Unfortunatly, in all my temptive, the btnStop cannot be pressed and the reading proceede till the end before I get again the control of the program. It is like the btnStop is freezed.
So the problem is: how to use the timecallback, or any other mean, to start a loop and stop it at any time?
Thanks,
Luigi
|
|
|
Re: How to set a TimeCallback [message #50669 is a reply to message #50667] |
Sun, 02 December 2018 09:42   |
|
Hi Luigi,
It sounds like Speak() is blocking, effectively freezing you GUI until it finishes. I couldn't find relevant docs for C API (my google-fu is weak today ), but the Visual Basic reference mentions that you need to pass some flag to actually make the call asynchronous. With a little luck, the C API could be the same 
Best regards,
Honza
|
|
|
|
Re: How to set a TimeCallback [message #50671 is a reply to message #50670] |
Sun, 02 December 2018 14:29   |
|
Well, from that example it looks, like you need the SPF_ASYNC flag, which I found described here.
No Set/KillTimeCallback should be necessary for this. Just launch the Speak with the async flag in Start button callback and stop it by Stop button callback, which calls something like Speak("", SPF_PURGEBEFORESPEAK, NULL).
Best regards,
Honza
PS: Just guessing based on the docs, I haven't tried any of this. I actually didn't even touch any windows API for years
|
|
|
|
Goto Forum:
Current Time: Tue Apr 29 03:02:43 CEST 2025
Total time taken to generate the page: 0.04299 seconds
|