/// <summary> /// Starts and runs the magic /// </summary> public void run() { //always initiate the HandProcessDispatcher BEFORE the FileSystemWatcher... HandProcessDispatcher hpd = new HandProcessDispatcher(); Task hpdTask = new Task(() => hpd.run()); hpdTask.Start(); Manager.runningTasks.TryAdd(hpdTask, true); FileSystemWatcher fsw = new FileSystemWatcher(); Task fswTask = new Task(() => fsw.run()); // Start this task only once for now! TODO: start this task multiple times to scan folders of multiple users on the same machine fswTask.Start(); Manager.runningTasks.TryAdd(fswTask, true); while (Manager.shutdown == false) { Thread.Sleep(1000); //this one has to do nothing... } // shutdown - wait for all tasks to finish... Stopwatch sw = new Stopwatch(); sw.Start(); while (Manager.runningTasks.Keys.Count > 0 && sw.Elapsed.Seconds < 3) { Thread.Sleep(100); //check every 100ms if all tasks have finished after 3 seconds -> hard shutdown } }