This is taken from bandalore sample code. But, you're right, a thread might make more sense.