本文介绍了任务取消最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说,我有一个处理器,其职责是将文件持久化回磁盘.这正在作为 Task 运行,同时观察 BlockingCollection< T> 来处理文件.

Lets say I have a processor who's job is to persist files back to the disk. This is running as a Task while observing a BlockingCollection<T> for files to process.

取消任务后,仍然有文件应保存到磁盘上,这样做的一个好习惯是什么?

When the task gets cancelled and there are still files which should be saved to the disk, what would be a good practice for doing so?

在退出之前让任务正确执行将是很方便的,尽管我不确定这是否与取消任务的原理相冲突(因为取消应尽可能快地进行).

It would be convenient to let the task right before exiting quickly write the files remaining back to the disk although I'm not sure if this conflicts with the philosophy of cancelling a task (since cancellation should happen as quick as possible).

另一种选择是在取消任务后执行第二步,该任务的任务是将剩余文件写入磁盘.

Another option is having a second procedure after cancelling the task who's job is to write the remaining files to the disk.

代码示例:

class FileProcessor
{
    private readonly BlockingCollection<Stream> input;

    public FileProcessor(BlockingCollection<Stream> input)
    {
        _input = input;
    }

    public Task Run(CancellationToken cancellationToken,
        BlockingCollection<Stream> input)
    {
        return Task.Factory.StartNew(() =>
        {
            foreach (Stream stream in
                        input.GetConsumingEnumerable(cancellationToken))
            {
                WriteToDisk(stream);
            }

            // Should I call WriteRemaining here or should I have
                    // a process running after this task exited which
                    // will call WriteRemaining
            WriteRemaining();
        });
    }

    public void WriteRemaining()
    {
        foreach (Stream stream in input)
        {
            WriteToDisk(stream);
        }
    }
}

我知道这是一个悬而未决的问题,应用程序/要求/要写入的文件量也起着一定作用,但我正在这里寻求一般性指南/最佳实践.

I know this is a bit of an open question, the application/requirements/amount of files to write also play a role but I'm seeking for the general guideline/best practices here.

推荐答案

取消是是与任务并行库一起使用时的合作动作,是的,建议取消是一种快速操作.

Cancellation is a cooperative action when working with the Task Parallel Library, and yes, cancelling is recommended to be a quick operation.

请记住,这是取消,而不是取消和清理.如果您由于取消而需要执行其他操作,那么这些操作应该发生在已取消的原始任务之外.

Remember, this is a cancellation, not a cancellation and cleanup. If you have extra operations that you need to perform as the result of a cancellation, then those operations should occur outside of the original task that was cancelled.

请注意,这不会阻止您致电 ContinueWith ,并在 Task ,它检查是否 IsCanceled 属性返回 true ,然后基于此执行清除.

Note that this doesn't stop you from calling ContinueWith and performing an operation in a new Task which checks to see if the IsCanceled property returns true and then performs the cleanup based on that.

此处的关键点是您不想阻止已取消的原始 Task ,但是您可以随意启动新的 Task 来执行任何清理工作您需要取消订单.

The key point here is that you don't want to block the original Task that was cancelled, but you are free to start a new Task to perform whatever cleanup you need to do as a result of the cancellation.

这篇关于任务取消最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:53