- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直看到在 CancellationTokenRegistration
结果上使用 Cancellation.Register
和 using
子句的代码:
using (CancellationTokenRegistration ctr = token.Register(() => wc.CancelAsync()))
{
await wc.DownloadStringAsync(new Uri("http://www.hamster.com"));
}
我知道你应该确保你 Dispose
一个 IDisposable
,但为什么它甚至实现了 IDisposable
?它必须释放什么资源?它唯一的方法是考虑平等。
如果您不处置
它会怎样?你泄露了什么?
最佳答案
此模式是确保自动调用 CancellationTokenRegistration.Unregister()
的便捷方式。它经常被 Stephen Toub 在他的 Parallel Programming with .NET 中使用。博客文章,例如here .
I get that you should make sure you Dispose an IDisposable, but whydoes it even implements IDisposable? what resources does it have torelease? The only methods it has regard equality.
IMO,最好的答案可以在 .NET 4 Cancellation Framework 中找到Microsoft 的 Mike Liddell 发表的文章:
When a callback is registered to a
CancellationToken
, the currentthread'sExecutionContext
is captured so that the callback will be runwith the the exact same security context . The capturing of thecurrent thread's synchronization context is optional can be requestedvia an overload ofct.Register()
if required. Callbacks are normallystored and then run when cancellation is requested, but if a callbackis registered after cancellation has been requested, the callback willrun immediately on the current thread, or viaSend()
on the currentSynchronizationContext
if applicable.When a callback is registered to a
CancellationToken
, the returnedobject is aCancellationTokenRegistration
. This is a light struct typethat isIDiposable
, and disposing this registration object causes thecallback to be deregistered. A guarantee is made that after theDispose()
method has returned, the registered callback is neitherrunning nor will subsequently commence. A consequence of this is thatCancellationTokenRegistration.Dispose()
must block if the callback iscurrently executing. Hence, all registered callbacks should be fastand not block for any significant duration.
Mike Liddell 的另一个相关文档是 "Using Cancellation Support in .NET Framework 4" (UsingCancellationinNET4.pdf) .
已更新,这是可验证的here in the Reference Source .
同样重要的是要注意,取消回调是通过 CancellationTokenSource
注册的,而不是通过 CancellationToken
注册的。因此,如果 CancellationTokenRegistration.Dispose()
的范围不正确,则注册将在父 CancellationTokenSource
对象的生命周期内保持事件状态。当异步操作的范围结束时,这可能会导致意外回调,例如:
async Task TestAsync(WebClient wc, CancellationToken token)
{
token.Register(() => wc.CancelAsync());
await wc.DownloadStringAsync(new Uri("http://www.hamster.com"));
}
// CancellationTokenSource.Cancel() may still get called later,
// in which case wc.CancelAsync() will be invoked too
因此,使用 using
(或使用 try/终于
).
关于c# - 为什么 CancellationTokenRegistration 存在以及它为什么实现 IDisposable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367695/
据我所知,这是一个公认的规则,如果你有一个具有 IDisposable 成员 m 的类 A,A 应该实现 IDisposable 并且它应该在其中调用 m.Dispose()。 我找不到令人满意的理由
我有一个父类和子类都需要实现IDisposable。 virtual(和 base.Dispose()?)调用应该在哪里发挥作用?当我只是重写 Dispose(bool disposing) 调用时,
我意识到这个问题正在寻找的查询不足以找到 IDisposable 实现的每一个小问题,但每个早期警告都很重要,所以我会尽我所能。 我想知道是否有人提出了 NDepend 的 CQL 查询,该查询将列出
我对 C# 还是比较陌生,只是在过去几天才接触到“IDisposables”。我可以掌握 using block 的概念来处理必须处理的对象,而无需手动记住调用 .Dispose() 方法 - 方便!
在我的应用程序中,我有一个每隔几秒创建一次的大对象。我用它做了一些工作,然后就不再需要它了。 我在任务管理器中看到,即使我没有对该对象的任何引用并且需要收集它,ram 大小也会增加。 在实现 IDis
考虑一下: interface IFoo : IDisposable { } class Program { static void Main() { var foo = GetFoo
我是这么认为的。但是请看一下 ASP.NET 中的内置类: public sealed class HttpPostedFile { public Stream InputStream { g
昨天,在我们的代码库上运行 Visual Studio 代码分析后,以下代码被突出显示为一个问题: using (var stringReader = new StringReader(someStr
我认为这个问题说明了一切。 谢谢。 最佳答案 如果对象拥有该资源,他们应该实现IDisposable。通常不需要将事物设置为 null,尽管它没有坏处。 如果您不拥有该资源,那么显然您不应该处置它,并
MSDN 文档和 StackOverflow 上的许多答案竭尽全力讨论如何正确实现 IDisposable,例如MSDN IDisposable , MSDN Implementing IDispos
如果一个类继承自一个本身实现了 IDisposable 的接口(interface),该类是否也应该实现 IDisposable? 例如 internal IMyInterface : IDispos
当 出现时,正确处理接口(interface)的默认实现的最佳方法是什么?接口(interface)不继承自 IDisposable ?例如,假设我想做 public class FooGetter
我有一个实现 IDisposable 的类,因为它有一个 IDisposable 的私有(private)成员字段“foo”(在构造函数中初始化)。我意外地收到了 CA2000 代码分析错误,它希望我
如果我有一个相当标准的抽象类,它允许注入(inject)一个 IDisposable 实例。但是,一些继承自此类的类不应处置注入(inject)的存储库,而其他类则应处置。显而易见的解决方案是有一个构
这个问题在这里已经有了答案: Declare IDisposable for the class or interface? (7 个答案) 关闭 4 年前。 我想问的是,如果类 A 继承自 Int
我有一个“永远”存在于应用程序中的 IDiposable 对象。在我的例子中是 SemaphoreSlim,但这个问题实际上适用于任何可以处置的对象。 我确信只要应用程序运行,我就需要一个对象。所以问
我有一堆 IDisposable查找表中的对象(现在是普通的旧字典<>),但为了简化代码并避免错误,我正在寻找一个“拥有”它所拥有的项目的集合类,并避免重新发明轮子 - 这样做一个类已经存在? 规范应
很久以前来自 C/C++ 的我仍然有确保所有资源都被正确清理的习惯。我总是确保在 IDisposable 类上调用 Dispose 并在包含一次性对象的类中实现 Dispose 模式。 但是,在我的环
如果我有实现 IDisposable 的接口(interface)(来自 apress 书中的示例)像这样 public interface IArchitectRepository : IDispo
问题是如何测试调用 Finalize 时对象释放资源的事实。类的代码: public class TestClass : IDisposable { public bool HasBeenDi
我是一名优秀的程序员,十分优秀!