I am doing something like this in python
class MyThread ( threading.Thread ): def run (s): try: s.wantQuit = 0 while(not s.wantQuit): button = raw_input() if button == 'q': s.wantQuit=1 except KeyboardInterrupt: s.wantQuit = 1 myThread = MyThread () myThread.start() a=5 while not myThread.wantQuit: print 'hey' if (a == 0): break; a = a-1; time.sleep(1) #''' sys.exit()
What happens is my app locks up for 5 seconds printing ‘hey’ (5 times) THEN i get the raw_input dialog. How can i have the dialog show up so i can quit anytime instead of when my loop runs out?
You mean the while loop runs before the thread? Well, you can’t predict this unless you synchronize it. No one guarantees you that the thread will run before or after that while loop. But if it’s being blocked for 5 seconds that’s akward – the thread should have been pre-empted by then.
Also, since you’re first use of wantToQuit is in the run() method, no one assures you that the thread has been started when you’re checking for it’s wantToQuit attribute in
while not myThread.wantToQuit.