I’m having trouble stopping a while-loop in the KeyListener function. Every 10 seconds the Timer function declares Active to be false. But still the while loop in the KeyListener function keeps running.
I can’t figure out why the loop keeps running; every cycle it should test whether Active is true, if it’s not (because after 10 seconds it should be switched off) the loop shouldn’t be running. But it does.
void KeyListener(bool Active)
{
cout << Active << endl; //debug
while (Active==true){
cout << "loop still running." << endl; //debug
Sleep(100);
for (int i=8;i<=190;i++){
if (GetAsyncKeyState(i) == -32767){
KeyWrite(i); // (turns the numbers into characters)
}
}
}
}
void Timer(void* pParams){
while (true){
Sleep(10000);
KeyListener(false); // Active = false
cout << "KeyListener(false)" << endl; // debug
}
}
int main()
{
_beginthread( Timer, 0, NULL );
KeyListener(true);
return 0;
}
After 10 seconds, the separate thread calls
KeyListener(false). This setsActivefalse for that function call. However, the originalKeyListener(true)function call is not affected. There is no way for a new call to affect a non-static local variable of the old call.