private static void ManageImportantTaskThread() { TaskThread tt = GetTaskThread(Thread.CurrentThread); while (GetTaskThread(ThreadRole.Standard) == null) { ; } while (GetTaskThread(ThreadRole.Standard) != null) { try { while (TaskQueue.Peek() == null) { Thread.Sleep(250); } Task t; tt.Busy = true; lock (taskLock) { t = ImportantTaskQueue.Dequeue(); } tt.Busy = false; PerformTask(t); } catch { tt.Busy = false; } //Queue is most likely empty, no real need to do anything. Thread.Sleep(250); } }
private static void ManageTaskDelayThread() { TaskThread tt = GetTaskThread(Thread.CurrentThread); while (GetTaskThread(ThreadRole.Standard) == null) { ; } while (GetTaskThread(ThreadRole.Standard).IsAlive) { try { tt.Busy = true; foreach (var v in DelayedTasks) { DelayedTasks[v.Key] -= 10; if (v.Value < 1) { TaskQueue.Enqueue(v.Key); DelayedTasks.Remove(v.Key); } } tt.Busy = false; Thread.Sleep(10); } catch { tt.Busy = false; } } }
/// <summary> /// Manage the current thread as a task thread. /// </summary> private static void ManageTaskThread() { TaskThread tt = GetTaskThread(Thread.CurrentThread); while (true) { try { while (TaskQueue.Peek() == null) { Thread.Sleep(250); } Task t; tt.Busy = true; lock (taskLock) { t = TaskQueue.Dequeue(); } PerformTask(t); tt.Busy = false; } catch { tt.Busy = false; } //Task invoked by another thread. No real need to do anything. Thread.Sleep(250); } }
public static bool KillTaskThread(TaskThread t) { if (t == null) { return(false); } t.Stop(); TaskThreads.Remove(t); return(true); }
/// <summary> /// Spawns a new task thread and adds it to the list of threads. /// </summary> /// <param name="role">The role of the thread.</param> public static void SpawnTaskThread(ThreadRole role) { TaskThread t; switch (role) //Set properties. { case ThreadRole.Delayed: t = TaskThread.SpawnTaskThread(role, ManageTaskDelayThread, TaskThreads.Count + 1, true); break; case ThreadRole.Important: t = TaskThread.SpawnTaskThread(role, ManageImportantTaskThread, TaskThreads.Count + 1, false); break; default: case ThreadRole.Standard: t = TaskThread.SpawnTaskThread(role, ManageTaskThread, TaskThreads.Count + 1, true); break; } TaskThreads.Add(t); t.Start(); //Start the new thread. }
/// <summary> /// Stops a task thread assocated with the givin role. /// </summary> /// <param name="role">The role of the thread to kill.</param> /// <returns>Whether the thread was killed or not.</returns> public static bool KillTaskThread(ThreadRole role) { TaskThread t = GetTaskThread(role); return(KillTaskThread(t)); }
public static bool KillTaskThread(TaskThread t) { if (t == null) return false; t.Stop(); TaskThreads.Remove(t); return true; }