当处置CancellationTokenSource? [英] When to dispose CancellationTokenSource?

查看:387
本文介绍了当处置CancellationTokenSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CancellationTokenSource 是反射一次性的,快看证明很有可能非托管资源 KernelEvent 的使用。它没有终结,所以如果我们不处理GC不会做到这一点。

The class CancellationTokenSource is disposable and quick look in reflector proves usage of very probably unmanaged resource KernelEvent. It has no finalizer, so if we do not dispose GC wont do that.

在另一方面,如果你看一下样品在 MSDN对消文章所有$ C $ ç段不这样做,只有一个除外。

On the other hand if you look at samples on MSDN Cancelation article all code snippets don't do that except one.

这似乎是很难找到正确的地点和时间做,在code。

It seems to be difficult to find correct place and time to do that in code.


  1. 您不能完成code。使用如果你不等待它与开始您的并行任务。它很有意义的取消仅如果你不等待。

  2. 当然你也可以在任务中添加 ContinueWith 的Dispose 通话,但它的路要走?

  3. 怎么样取消PLINQ查询,不同步回来,但只是在最后somenthing?比方说, .ForaAll(X => Console.Write(X))?

  4. 是可重用?可重复使用多次调用同样的道理,然后用主机组件一起丢弃它,让我们说的UI控件?

  1. You can not wrap code starting your parallel task with using if you do not wait for it. And it makes sense to have cancelation only if you do not wait.
  2. Of course you can add ContinueWith on task with a Dispose call, but it that the way to go?
  3. What about cancelable PLINQ queries, which do not synchronize back, but just do somenthing at the end? Let's say .ForaAll(x=> Console.Write(x))?
  4. Is it reusable? Can reuse the same token for several calls and then dispose it together with host component, let's say UI control?

由于它没有像重置方法来清理 IsCancelRequested 令牌提起我想这也不是可重复使用的,因此每次启动一个任务(或PLINQ查询),你应该创建一个新的。是真的吗?如果是的话,我的问题是:如何应对的Dispose 上一颗颗 CancelationTokenSource 情况下,正确的和推荐的策略是什么?

Because it has not something like Reset method to clean-up IsCancelRequested and Token filed I would suppose it's nor reusable, thus every time you start a task (or a PLINQ query) you should create a new one. Is it true? If yes, my question is what is the correct and recommended strategy to deal with Dispose on those many CancelationTokenSource instances?

推荐答案

在谈到它是否真的有必要调用Dispose CancellationTokenSource ......我在我的项目有内存泄漏,它原来是的CancellationToken(来源)是问题...

Speaking about if it's really necessary to call Dispose on CancellationTokenSource... I had a memory leak in my project and it turned out that CancellationToken(Source) was the problem...

我的项目有一个服务,即不断读取数据库,并打完不同的任务,而我经过链接取消标记到我的工作人员,让他们过完处理数据即使,取消标记不处置,造成内存在泄露我的情况。

my project has a service, that is constantly reading database and fires off different tasks, and I was passing linked cancellation tokens to my workers, so even after they had finished processing data, cancellation tokens weren't disposed, which caused memory leak in my case.

和MSDN <一个href=\"http://msdn.microsoft.com/en-us/library/dd997364.aspx\">http://msdn.microsoft.com/en-us/library/dd997364.aspx实际上规定很清楚:
请注意,您必须调用Dispose链接的标记源,当你用它做。有关更完整的示例,请参见如何:侦听多个取消请求

and MSDN http://msdn.microsoft.com/en-us/library/dd997364.aspx actually states it clearly: Notice that you must call Dispose on the linked token source when you are done with it. For a more complete example, see How to: Listen for Multiple Cancellation Requests.

我用ContinueWith做

I used ContinueWith to do it

这篇关于当处置CancellationTokenSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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