超时时取消任务中的任务。C#

亚历山大大帝

我正在尝试运行任务集合。我有一个procesing object model,我不知道的属性。这就是为什么我创建了这个班级的孩子。我有一些ProcesingTask物件。这是我的代码:

 public sealed class ProcessingTask : ProcessingObject
    {
        private CancellationTokenSource cancelToken;
        private System.Timers.Timer _timer;
        public int TimeOut {private get; set; }
        public int ProcessObjectID { get; private set; }         
        public Task ProcessObjectTask { get; private set; }
        public QueueObject queueObject { private get; set; }
        public ProcessingTask(int processObjectID)
        {          
            this.ProcessObjectID = processObjectID;
            ResetTask();               
        }
    private void InitialTimeOut()
    {
        _timer = new System.Timers.Timer(TimeOut);
        _timer.Elapsed += new ElapsedEventHandler(TimedOut);
        _timer.Enabled = true;
    }
    private void TimedOut(object sender, ElapsedEventArgs e)
    {
         cancelToken.Cancel();
         Console.WriteLine("Thread {0} was timed out...", ProcessObjectID);
        _timer.Stop();            
    }
    public void ResetTask()
    {
        cancelToken = new CancellationTokenSource();
        ProcessObjectTask = new Task(() => DoTaskWork(), cancelToken.Token);            
    }
    public void DoTaskWork()
    {
        InitialTimeOut();     
        Random rand = new Random();
        int operationTime = rand.Next(2000, 20000);               
        Thread.Sleep(operationTime);
        _timer.Stop();          
         Console.WriteLine("Thread {0} was finished...", ProcessObjectID);            
    }       
}

public class CustomThreadPool
    {
        private IList<ProcessingTask> _processingTasks;      
        public CustomThreadPool(List<ProcessingTask> processingObjects)
        {           
            this._processingTasks = processingObjects;
        }
        public void RunThreadPool(Queue<QueueObject> queue, int batchSize)
        {
            for (int i = 1; i <= batchSize; i++)
            {
                QueueObject queueObject = queue.ToArray().ToList().FirstOrDefault();

                ProcessingTask task = _processingTasks.FirstOrDefault(x => x.ProcessObjectID == queueObject.QueueObjectId);                
                task.queueObject = queue.Dequeue();
                task.TimeOut = 3000;
                task.ProcessObjectTask.Start();   
            }
        }

        public void WaitAll()
        {
            var tasks = _processingTasks.Select(x => x.ProcessObjectTask).ToArray();           
            Task.WaitAll(tasks); 
        }
    }

DoTaskWork()如果运行时间超时,我需要停止我正在尝试使用timerCancellationToken但是之后DoTaskWork()仍然做他的工作TimedOut()有什么办法解决这个问题?

杰尔吉·科塞格(GyörgyKőszeg)

尽管您发送了取消信号,但是您并没有对它做任何事情 DoTaskWork

public void DoTaskWork()
{
    InitialTimeOut();     
    Random rand = new Random();
    int operationTime = rand.Next(2000, 20000);

    // Thread.Sleep(operationTime); // this imitates a non-responsive operation

    // but this imitates a responsive operation:
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
    while (!cancelToken.IsCancellationRequested
      && stopwatch.ElapsedMilliseconds < operationTime)
    {
        Thread.Sleep(10);
    }

    _timer.Stop();          
     Console.WriteLine("Thread {0} was finished...", ProcessObjectID);            
}       

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章