在Task的调用Dispose方法
问题说明
大家好。
我来这里有一个与Task类中的Dispose方法相关的问题。
这个方法有多强制?每当我完成任务实例的工作时,我是否需要调用它。
我在Reflector的帮助下调查了Dispose方法,我发现在内部调用它时它会执行一些清理工作特别针对ManualResetEvent的东西。
Hi guys.
I come here with one question related to Dispose method in Task class.
How mandatory this method is ? And do i need to invoke it every time when i finished to work with task instance.
I have investigated Dispose method with help of Reflector, and i found that internally when we calling it it perform some clean up stuff especially against ManualResetEvent.
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if ((this.Options & 0x4000) != TaskCreationOptions.None)
{
return;
}
if (!this.IsCompleted)
{
throw new InvalidOperationException(Environment.GetResourceString("Task_Dispose_NotCompleted"));
}
ContingentProperties contingentProperties = this.m_contingentProperties;
if (contingentProperties != null)
{
ManualResetEventSlim completionEvent = contingentProperties.m_completionEvent;
if (completionEvent != null)
{
contingentProperties.m_completionEvent = null;
if (!completionEvent.IsSet)
{
completionEvent.Set();
}
completionEvent.Dispose();
}
}
}
this.m_stateFlags |= 0x40000;
}
主要关注的是下一个:
我常常使用这样的结构:
The main concern about that lies in next:
Quite often i use such construction:
Task.Factory.StartNew(()=>{ .. some stufff });
在这种情况下调用Dispose有多重要???
How important in such case to invoke Dispose ???
正确答案
这取决于。
哪个可能没有很有帮助,但是......
你应该在显式实现它的任何对象上调用Dispose,因为这个想法是对象包含或使用的资源稀缺供应,或使用大量空间 - 是记忆或其他东西。如果没有调用Dispose(显式地,或者通过使用块将其封装在中),则在调用垃圾收集器之前不会释放资源并确定不再需要对象 - 此时它将在对象上调用Dispose。因为这只发生在内存开始不足的正常情况下,所以不会发生几个小时,几天或几周 - 或者直到应用程序终止。
如果你的对象没有实现IDisposable那么你就不需要明确地调用Dispose。
你的一些对象(绝不是全部) Dispose应该是SqlCommand,SqlConnection,所有图形对象,字体,画笔,位图等。
It depends.
Which may not be much help, but...
You should call Dispose on any object which explicitly implements it, because the idea is that the object contains or uses resources which are in scarce supply, or which use a lot of "space" - be that memory or something else. If Dispose isn''t called (either explicitly, or by enclosing it in ausing
block) then the resources are not released until the Garbage Collector is called in and decides that the object is no longer needed - at which point it will call Dispose on the object. Because this only happens in normal circumstances when memory starts to run low, it man not happen for several hours, or days, or weeks - or until the application terminates.
If your objects don''t implement IDisposable then you shouldn''t need to explicitly call Dispose.
Some of the objects (and by no means all) which you should Dispose are SqlCommand, SqlConnection, all Graphics objects, Fonts, Brushes, Bitmaps and so forth.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfkakh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20