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

时间:2015-09-09 07:54:56

标签: c# task-parallel-library

我试图运行任务集合。我有一个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()仍在继续工作。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

虽然你发送了取消信号,但你在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);            
}