Possible Duplicate: Proper use of the IDisposable interface I have a class that has both managed and unmanaged resources. I am using IDisposable to release un-managed resources. Should I release managed resources in the dispose method? Or I can leave it to GC to release managed resources? If you have a look at the following documentation you will find the line: Free any disposable reso
可能重复: 正确使用IDisposable接口 我有一个拥有托管和非托管资源的类。 我正在使用IDisposable释放未管理的资源。 我应该在dispose方法中释放托管资源吗? 或者我可以让它发布到GC来释放托管资源? 如果你看看下面的文档,你会发现这一行: 在Dispose方法中释放类型拥有的任何可支配资源。 所以在你的dispose方法中,你应该处理也实现IDisposable的管理资源。 如果一个对象没有实现这个,你不必处理它。 我建
Possible Duplicate: Proper use of the IDisposable interface I tried to find an actual answer to my question from books, internet and on stackoverflow, but nothing has helped me so far, so hopefully I can word my issue exact enough to make sense. In general I always found the same basic usage of how to free memory, which is approx. as follows and I do understand the code itself: public cla
可能重复: 正确使用IDisposable接口 我试图从书籍,互联网和计算器上找到我的问题的实际答案,但迄今为止没有任何帮助我,所以希望我可以将我的问题准确无误地讲出来。 总的来说,我总是发现如何释放内存的基本用法,这是近似的。 如下所示,我确实了解代码本身: public class MyClass : IDisposable { bool disposed = false; public void Dispose() { if (!disposed) { Dispose(
Possible Duplicate: Proper use of the IDisposable interface "IDisposable Interface" article tells: The primary use of this interface is to release unmanaged resources Why? Why only unmanaged? Whole my life I thought its PRIMIRALY use is to release ANY resources: managed (connections to DBs, services proxies, etc) and unmanaged (if they are used in application). PS I belie
可能重复: 正确使用IDisposable接口 “IDisposable接口”文章讲述: 该接口的主要用途是释放非托管资源 为什么? 为什么只有非托管? 整个我的生活,我认为它的PRIMIRALY使用是释放任何资源:托管(连接数据库,服务代理等)和非托管(如果他们在应用程序中使用)。 PS 我相信这个主题已经有问题了,但是找不到它们。 数据库的底层连接不受管理,文件句柄和许多其他低级别的o / s对象也是如此。 他们是非托管的
This question already has an answer here: Proper use of the IDisposable interface 18 answers If you mean unmanaged objects then yes, you should be implementing it whenever you have one or more unmanaged resource you are handling in your class. You should also be using the pattern when you are possibly holding on to objects that are IDisposable themselves, and make sure to dispose of them whe
这个问题在这里已经有了答案: 正确使用IDisposable接口18的答案 如果您的意思是非托管对象,那么您应该在您有一个或多个您的班级处理的非托管资源时实施它。 当你可能持有IDisposable本身的对象时,你也应该使用这个模式,并且确保在你的类被丢弃时处置它们。 (同意这个问题已经被问及足够多的时间,以便在打印的时候使用小墨盒来打印小墨盒...) 虽然每个人都提到(非托管)资源,但我还有一件事要补充:当我需要消
There are many questions on this subject on SO, but I haven't found one that covers what I specifically need to understand. One of my developers wrote this code: // // ValidationDataTable is a typed DataTable, generated by the Framework ValidationDataTable validationTable; using (ValidationTableAdapter adapter = new ValidationTableAdapter ()) { using (validationTable
在这个主题上有很多关于这个问题的问题,但我还没有找到一个涵盖我特别需要了解的问题。 我的一位开发人员编写了这段代码: // // ValidationDataTable is a typed DataTable, generated by the Framework ValidationDataTable validationTable; using (ValidationTableAdapter adapter = new ValidationTableAdapter ()) { using (validationTable = adapter.GetData()) { } } datafeedValid
I have been fixing some memory leak issues in a winforms application and noticed some disposable objects that are not Disposed explicitly (developer hasn't called Dispose method). Implementation of Finalize method also doesn't help because it doesn't go in if (disposing) clause. All the static event unregistering and collection clearing have been put in if (disposing) clause. The b
我一直在修复winforms应用程序中的一些内存泄漏问题,并注意到一些不显式放弃的对象(开发人员没有称为Dispose方法)。 Finalize方法的实现也没有帮助,因为它没有进入if (disposing)子句。 所有的静态事件注销和收集清除都已经放在if (disposing)条款中。 最好的做法是调用Dispose,如果对象是一次性的,但不幸的是,这种情况有时会发生 如果有非托管对象,静态事件处理程序和一些托管集合,这些集合在处置时需要清除。
I need some advice on the implementation of the Dispose method. In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common base class ScreenObject. My preview manager contain a single object reference to a ScreenGrid which is the grid object for the entire preview area.
我需要一些关于Dispose方法实现的建议。 在我们的应用程序中,用户设计自己的UI。 我有一个预览窗口,显示UI的外观。 在这个用户界面中绘制的所有对象最终都来自一个公共基类ScreenObject。 我的预览管理器包含对ScreenGrid的单个对象引用,这是整个预览区域的网格对象。 问题#1 我的一些派生屏幕类保存非托管资源,如数据库连接,位图图像和WebBrowser控件。 这些类需要处理这些对象。 我在基础ScreenObject基类中
I like the using(){} statement for its control of scope and for readability. Not only can you create objects, use them and dispose them neatly, but you can also use it like this: Suppose myInstance is an instance of MyClass from some other place in code - ie a method parameter or something using (var t = myInstance) { t.Foo= "Hello"; t.Bar= "World"; ... } Definition of MyClass:
我喜欢using(){}语句来控制范围和可读性。 您不仅可以创建对象,使用它们并整齐地处理它们,而且还可以像这样使用它: 假设myInstance是来自代码中其他地方MyClass的一个实例 - 也就是方法参数或其他东西 using (var t = myInstance) { t.Foo= "Hello"; t.Bar= "World"; ... } MyClass的定义: public class MyClass : IDisposable { public string Foo {get; set;} public string Bar {get; set;}
For the equivalent mechanism in C++ (the destructor), the advice is that it should usually not throw any exceptions. This is mainly because by doing so you might terminate your process, which is only very rarely a good strategy. In the equivalent scenario in .NET ... A first exception is thrown A finally block is executed as a result of the first exception The finally block calls a Dispo
对于C ++(析构函数)中的等价机制,建议是通常不应抛出任何异常。 这主要是因为通过这样做你可能会终止你的过程,这很少是一个好策略。 在.NET中的等效方案中... 抛出第一个异常 作为第一个异常的结果,finally块被执行 finally块调用Dispose()方法 Dispose()方法抛出第二个异常 ...你的过程不会立即终止。 但是,由于.NET不会将第一个异常替换为第二个异常,因此会丢失信息。 因此,调用堆栈上方的catch块
For example: Queue<System.Drawing.SolidBrush> brushQ = new Queue<System.Drawing.SolidBrush>(); ... brushQ.Clear(); If I don't explicitly dequeue each item and dispose of them individually, do the remaining items get disposed when calling Clear()? How about when the queue is garbage collected? Assuming the answer is "no", then what is the best practice? Do you have
例如: Queue<System.Drawing.SolidBrush> brushQ = new Queue<System.Drawing.SolidBrush>(); ... brushQ.Clear(); 如果我没有明确列出每个项目并单独处理它们,那么在调用Clear()时是否会处理剩余的项目? 如何排队等待垃圾回收? 假设答案是“否”,那么最佳做法是什么? 你是否必须总是迭代队列并处理每个项目? 这可能会变得很难看,特别是如果你必须尝试......每次处理周围,以防万一抛出异常。 编