Refactor as a task queue with a worker thread.