Use Thread for mainloop instead of iterating manually.