Gracefully quit the thread to avoid race conditions
On Close events, ensure the trainTool stops first to clean up