Exemplo n.º 1
0
        public async void Start()
        {
            IsExcuting = true;
            List <Task> tasks = new List <Task>();

            // await Task.Run(() =>
            //{
            for (int i = 0; i < Configs.RefreshThreadCount; i++)
            {
                tasks.Add(Task.Factory.StartNew(Excute, TaskCreationOptions.LongRunning));
                //用Task.Run会变卡,可能是因为共用了线程;
                //之前的方法会卡UI可能也是这个原因
                //tasks.Add(Task.Run(() => Excute()));
                await Task.Delay(500 / Configs.RefreshThreadCount);
            }
            //});
            await Task.WhenAll(tasks);

            TaskStopped?.Invoke(this, new EventArgs());
        }
Exemplo n.º 2
0
 /// <summary>
 /// 启动服务
 /// </summary>
 public async Task Start()
 {
     cts = new CancellationTokenSource();
     await Task.Run(() =>
     {
         int times = 0;
         TaskStarted?.Invoke(this, new TaskEventArgs(TaskName));
         while (!cts.Token.IsCancellationRequested)
         {
             times++;
             if (times *Interval >= TaskCheckInterval)
             {
                 Log.Information($"{TaskName}正在运行");
                 times = 0;
             }
             Repository = new AppRepository();
             lastTime   = DateTime.Now;
             try
             {
                 Run();
             }
             catch (Exception ex)
             {
                 Log.Error(ex, $"{TaskName}抛出异常");
             }
             try
             {
                 Task.Delay(Interval).Wait(cts.Token);
             }
             catch (OperationCanceledException)
             {
             }
         }
         Log.Warning($"{TaskName}已取消");
         TaskStopped?.Invoke(this, new TaskEventArgs(TaskName));
     }, cts.Token);
 }
Exemplo n.º 3
0
        private void CheckAndDo()
        {
            while (!stopping)
            {
                if (!tasks.IsEmpty)
                {
                    if (!IsExcuting)
                    {
                        IsExcuting = true;
                        ProcessStatusChanged?.Invoke(this, new ProcessStatusChangedEventArgs(true));
                    }
                    if (tasks.Count > TasksMaxCount)
                    {
                        Stack <Action> temp = new Stack <Action>();
                        for (int i = 0; i < TasksMaxCount; i++)
                        {
                            if (tasks.TryPop(out Action act))
                            {
                                temp.Push(act);
                            }
                        }
                        tasks.Clear();
                        while (temp.Count > 0)
                        {
                            tasks.Push(temp.Pop());
                        }
                    }
                    Debug.WriteLineIf(DebugSwitch.TaskQueue, "Task count is " + tasks.Count);
                    List <Action> currentTasks = new List <Action>();
                    //后进来的先处理,每次处理n倍的线程数份,这样可以让后进来的尽快处理
                    for (int i = 0; i < Configs.RefreshThreadCount * 3; i++)
                    {
                        if (tasks.Count > 0)
                        {
                            if (tasks.TryPop(out Action act))
                            {
                                currentTasks.Add(act);
                            }
                        }
                    }

#if (SingleThread && DEBUG)
                    foreach (var task in currentTasks)
                    {
                        if (stopping)
                        {
                            break;
                        }
                        try
                        {
                            //执行任务
                            task();
                        }
                        catch (Exception ex)
                        {
                        }
                    }
#else
                    ParallelOptions opt = new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = Configs.RefreshThreadCount
                    };
                    Parallel.ForEach(currentTasks, opt, (t2, e) =>
                    {
                        if (stopping)
                        {
                            e.Stop();
                        }
                        try
                        {
                            //执行任务
                            t2();
                        }
                        catch (Exception ex)
                        {
                        }
                    });
#endif
                }
                else
                {
                    if (IsExcuting)
                    {
                        Debug.WriteLineIf(DebugSwitch.TaskQueue, "Task is empty");
                        IsExcuting = false;
                        ProcessStatusChanged?.Invoke(this, new ProcessStatusChangedEventArgs(false));
                    }
                    Thread.Sleep(500);
                }
            }
            TaskStopped?.Invoke(this, new EventArgs());
            IsExcuting = false;
        }
Exemplo n.º 4
0
 protected virtual void OnTaskStopped()
 {
     TaskStopped?.Invoke(this);
 }
 private void OnTaskStopped()
 {
     TaskStopped?.Invoke(this, EventArgs.Empty);
 }