Fix problem with thread initialization when run in the background