Threading questions
Posted: Mon Apr 21, 2008 3:16 pm
1) hmm, I'm probably doing something dumb, but just wondering is someone could shed some light on this.
When debugflag is set to false, the PrintF statements inside the thread (for_real function), don't get printed until the thread is closed.
When I do set debugflag, the main loop prints a line every second and
the output generated in the thread ends up in between the debug lines.
(as I would expect it; ie during the running of the thread).
Why doesn't it output the text when the thread is running and debugflag is off ?
2) I added my 'automisation' routine in a different thread, so that I can
check from inside the thread for certain global variables.. ie bind a hotkey
to a function that sets a bool to TRUE to make the thread abort.
For some strange reason when I hit the hotkey in the middle of the thread
execution, it will only set the bool after the thread has finished..
Could anybody give me a small example of how a hotkey stops a thread?
Do I need 2 threads to accomplish this ?
- Code: Select all
/////////////////////////////////////////////////////////////////////////////////////////////
VOID mainthread() {
Clear();
DWORD dwParm = 0;
ERRORCODE = 0;
int counter = 0;
HANDLE hThread = CreateThread("for_real", dwParm);
if ( !hThread ) {
PrintF("[ERROR] Failed to create the run thread!");
return;
}
PrintF("[RUN] Started the run in a thread..");
while ( ERRORCODE < 1 ) {
Sleep(1000);
counter++;
if ( debugflag ) PrintF("[DEBUG] We are currently %u seconds into the run.", counter);
if (counter > 180) {
ERRORCODE = 2;
PrintF("Errorcode was nr 2");
}
}
CloseHandle( hThread );
}
When debugflag is set to false, the PrintF statements inside the thread (for_real function), don't get printed until the thread is closed.
When I do set debugflag, the main loop prints a line every second and
the output generated in the thread ends up in between the debug lines.
(as I would expect it; ie during the running of the thread).
Why doesn't it output the text when the thread is running and debugflag is off ?
2) I added my 'automisation' routine in a different thread, so that I can
check from inside the thread for certain global variables.. ie bind a hotkey
to a function that sets a bool to TRUE to make the thread abort.
For some strange reason when I hit the hotkey in the middle of the thread
execution, it will only set the bool after the thread has finished..
Could anybody give me a small example of how a hotkey stops a thread?
Do I need 2 threads to accomplish this ?