Stop listening thread and wait for it to terminate.