最佳实践LongRunning任务创建 [英] Best Practice LongRunning Task creation

查看:162
本文介绍了最佳实践LongRunning任务创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很好的设计,需要使用任务API在.NET 4.0中运行一个后台线程?我唯一​​担心的是,如果我们要取消这项任务我会怎么做呢?我知道我可以只设置 ProgramEnding ,但我知道有一个的CancellationToken 任务的API中。

Is this a good design for a background thread that needs to be run using the Task API in .Net 4? My only concern is if we want to cancel that task how I would do it? I know I can just set ProgramEnding to true but I know there is a CancellationToken in the Task API.

这是code只是一个例子样品,这样一个线程将被添加到集合而另一个线程会从它删除。任务是设置为LongRunning,因为这需要连续运行,而在程序运行

This is just an example sample of code so that one thread will be adding to a collection and another thread will be removing from it. The Task is setup as LongRunning as this needs to be running continuously whilst the program is running

private void RemoveFromBlockingCollection()
{
    while (!ProgramEnding)
    {
       foreach (var x in DataInQueue.GetConsumingEnumerable())
       {
          Console.WriteLine("Task={0}, obj={1}, Thread={2}"
                          , Task.CurrentId, x + " Removed"
                          , Thread.CurrentThread.ManagedThreadId);
       }
    }
}

private void button1_Click(object sender, EventArgs e)
{
   DataInQueue = new BlockingCollection<string>();
   var t9 = Task.Factory.StartNew(RemoveFromBlockingCollection
                                 , TaskCreationOptions.LongRunning);

   for (int i = 0; i < 100; i++)
   {
     DataInQueue.Add(i.ToString());
     Console.WriteLine("Task={0}, obj={1}, Thread={2}", 
                       Task.CurrentId, i + " Added", 
                       Thread.CurrentThread.ManagedThreadId);
     Thread.Sleep(100);
   }
   ProgramEnding = true;
}

更新:我发现,我可以删除ProgramEnding布尔和使用DataInQueue.CompleteAdding这由此带来的线程结束

UPDATE: I have found that I can remove the ProgramEnding boolean and use DataInQueue.CompleteAdding which which bring the thread to an end.

推荐答案

正如你已经提到的,你可以使用的CancellationToken。做这种方式:

As you already mentioned, you can use the CancellationToken. Do it this way:

var cancellationTokenSource = new CancellationTokenSource();
Task.Factory.StartNew(RemoveFromBlockingCollection
                      , TaskCreationOptions.LongRunning
                      , cancellationTokenSource.Token);  

在你随后的code,你可以取消任务:

And later in your code, you can cancel the task with:

cancellationTokenSource.Cancel();

在您的长时间运行的任务,你可以问的道理,如果有人要求取消:

In your long running task you can ask the token, if cancellation was requested:

if (cancellationTokenSource.Token.IsCancellationRequested)

这篇关于最佳实践LongRunning任务创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆