Ejemplo n.º 1
0
        private void ReturnTask(ThreadTask task)
        {
            task.AssignThread(-1);
            task.SetProgress(0);

            lock (queuedTasks) {
                //queuedTasks.Insert(0, task);
                queuedTasks.Add(task);
                queuedTasks.Sort(Compare);
            }
        }
Ejemplo n.º 2
0
        public ThreadTask(TaskManager taskManager, string name, Action <ThreadTask> task, object data = null)
        {
            TaskManager = taskManager;
            ThreadID    = -1;
            Task        = task;
            Progress    = 0;
            Data        = data;
            DependsOn   = new ThreadTask[0];

            StartTime = Stopwatch.GetTimestamp();
            Name      = name;
        }
Ejemplo n.º 3
0
 public ThreadTask QueueTask(string name, Action <ThreadTask> taskCallback, object data = null)
 {
     lock (queuedTasks) {
         ThreadTask nTask = new ThreadTask(this, name, taskCallback, data);
         queuedTasks.Add(nTask);
         if (autoSort)
         {
             queuedTasks.Sort(Compare);
         }
         return(nTask);
     }
 }
Ejemplo n.º 4
0
 public int Compare(ThreadTask a, ThreadTask b)
 {
     return((a.Priority).CompareTo(b.Priority));
 }
Ejemplo n.º 5
0
        private void ThreadCallback(object state)
        {
            ThreadData tData = (ThreadData)state;

            ConsoleU.WriteLine($"[{tData.ThreadID}] THREAD {tData.ThreadID} ON", ConsoleColor.Green, OutputLevel.Medium);

            for (; ;)
            {
                long       startTime = Stopwatch.GetTimestamp();
                ThreadTask task      = GrabTask(startTime);

                if (task != null)
                {
                    tData.CurrentTask = task;

                    task.AssignThread(tData.ThreadID);

                    // execute task
                    task.Task(task);

                    long   endTime  = Stopwatch.GetTimestamp();
                    bool   doLog    = false;
                    double sElapsed = ((endTime - startTime) / (double)Stopwatch.Frequency) * 1000.0;
                    if (sElapsed > MinTimeTaskLog)
                    {
                        doLog = true;
                        ConsoleU.WriteLine($"[{tData.ThreadID}] TASK {task}", ConsoleColor.Green, OutputLevel.Medium);
                    }

                    // return to queued if its not finished
                    if (!task.Finished)
                    {
                        bool printReturn = doLog;

                        if (printReturn)
                        {
                            if (task.DependsOn.Length > 0)
                            {
                                string depends = "";
                                bool   first   = true;
                                for (int i = 0; i < task.DependsOn.Length; i++)
                                {
                                    ThreadTask depend = task.DependsOn[i];
                                    string     name   = depend.Finished ? $"{depend.Name}(DONE)" : depend.Name;
                                    depends += !first ? (", " + name) : name;
                                    if (first)
                                    {
                                        first = false;
                                    }
                                }

                                if (task.NeedReexecution)
                                {
                                    ConsoleU.WriteLine($"[{tData.ThreadID}] RET TASK: {task} FOR {depends} AND RE-EXECUTION ({task.ReexecutionMessage})", ConsoleColor.Yellow, OutputLevel.Medium);
                                }
                                else
                                {
                                    ConsoleU.WriteLine($"[{tData.ThreadID}] RET TASK: {task} FOR {depends}", ConsoleColor.Yellow, OutputLevel.Medium);
                                }
                            }
                            else if (task.NeedReexecution)
                            {
                                ConsoleU.WriteLine($"[{tData.ThreadID}] RET TASK: {task} FOR RE-EXECUTION ({task.ReexecutionMessage})", ConsoleColor.Yellow, OutputLevel.Medium);
                            }
                            else
                            {
                                ConsoleU.WriteLine($"[{tData.ThreadID}] RET TASK: {task} FOR UNKNOWN REASON", ConsoleColor.Red, OutputLevel.High);
                            }
                        }

                        task.ResetToReexecute();
                        ReturnTask(task);
                    }
                    else
                    {
                        ConsoleU.WriteLine($"[{tData.ThreadID}] FINISH TASK: {task} ELAPSED: {(sElapsed / 1000.0)}s", ConsoleColor.Green, OutputLevel.Medium);
                    }

                    //tData.CurrentTask = null; // dont remove so we can read the last task
                }

                //Thread.Sleep(100);
                //Thread.Sleep(1);// 1ms sleep
                Thread.Sleep(TimeSpan.FromTicks(1));// 1 tick sleep

                if (finished)
                {
                    break;
                }
            }
        }