idisposable

Why doesn't Thread implement IDisposable?

I noticed that System.Threading.Thread implements a finalizer but not IDisposable. The recommended practice is to always implement IDisposable when a finalizer is implemented. Jeffrey Richter wrote that the guideline is "very important and should always be followed without exception". So why doesn't Thread implement IDisposable? It seem...

How can disposable class detect whether there is an exception in progress?

I have a class that implements IDisposable public class Foo: IDisposable { public void Dispose() { // do the disposing } } Then I have a method that uses the class in the following manner: void Bar() { using (var f = new Foo()) { // do whatever } } When the code leaves the using {...} boundary, the ...

How to dispose <asp:ObjectDataSource>

Hello, how can I dispose an <asp:ObjectDataSource>? I mean, there is no code behind and in aspx file there is this: <asp:ObjectDataSource ID="CategoryDataSource" runat="server" SelectMethod="GetCategoriesFilter" TypeName="BLL.CategoryBLL"> </asp:ObjectDataSource> Class BLL.CategoryBll implements IDisposable. Do I have to di...

Do you have to dispose of IDisposable objects before you repopulate them?

Assuming I have a method in my command architecture pattern that alters the contents of graphics path like so: (GraphicsPath is IDisposable) (this is purely an untested, quick example) public void DoSomething(ref GraphicsPath path) { if(path != null) { List<PointF> pts = new List<PointF>(); foreach(PointF pt in p...

Is it important to dispose SolidBrush and Pen?

I recently came across this VerticalLabel control on CodeProject. I notice that the OnPaint method creates but doesn't dispose Pen and SolidBrush objects. Does this matter, and if so how can I demonstrate whatever problems it can cause? EDIT This isn't a question about the IDisposable pattern in general. I understand that callers s...

Calling Dispose() vs when an object goes out scope/method finishes

Hi, I have a method, which has a try/catch/finall block inside. Within the try block, I declare SqlDataReader as follows: SqlDataReader aReader = null; aReader = aCommand.ExecuteReader(); In the finally block, the objects which are manually disposed of are those which are set at the class leve...

How should I inherit IDisposable?

Class names have been changed to protect the innocent. If I have an interface named ISomeInterface. I also have classes that inherit the interface, FirstClass and SecondClass. FirstClass uses resources that must be disposed. SecondClass does not. So the question is, where should I inherit from IDisposable? Both of the following opt...

.NET IDisposable inline temps

I use code rush and refactor pro (highlight possible code issues and so on like ReSharper) and they were telling me I had undisposed locals (that implemented IDisposable). So I changed the code to this with two using statements: using (Reports.StudentRegisters.StudentQueriesDataTable studentDataTable = new Reports.StudentRegisters.Stude...

C# - What does "destructors are not inherited" actually mean?

Section 10.13, Destructors, of the C# Language Specification 3.0 states the following: Destructors are not inherited. Thus, a class has no destructors other than the one which may be declared in that class. The Destructors section of the C# Programming Guide contains an example demonstrating how destructors in an inheritance hierar...

How to properly dispose of a WebResponse instance?

Normally, one writes code something like this to download some data using a WebRequest. using(WebResponse resp = request.GetResponse()) // WebRequest request... using(Stream str = resp.GetResponseStream()) ; // do something with the stream str Now if a WebException is thrown, the WebException has a reference to the WebResp...

What's the best way of returning constructed IDisposables safely?

Edit: Two options shown below. If you're just using the functionality that an IDisposable provides, the aptly named using clause works fine. If you're wrapping an IDisposable in an object, the containing object itself needs to be IDisposable and you need to implement the appropriate pattern (either a sealed IDisposable class, or the me...

How to correctly use temporary storage in Application

Hi, I'm in the need again to manage a temporary folder where parts of our application store documents, e.g. between printing and importing to a dms. Those files should be deleted on application shutdown and ideally on application start as well, just in case something went wrong. I just thought of a simple class implementing IDisposabl...

Should a class with a Thread member implement IDisposable?

Let's say I have this class Logger that is logging strings in a low-priority worker thread, which isn't a background thread. Strings are queued in Logger.WriteLine and munched in Logger.Worker. No queued strings are allowed to be lost. Roughly like this (implementation, locking, synchronizing, etc. omitted for clarity): public class Log...

Implementation specific methods on an interface - how can I avoid them?

We have an interface that is passed to the constructor using an IoC container We have multiple classes that implement this interface. The problem is, some (not all) of the implementations need to be cleaned up, preferably using the IDisposable interface. Since not all of the implementations will need the "Dispose" method, do I includ...

How to unit test a method with a `using` statement?

How can I write a unit test for a method that has a using statement? For example let assume that I have a method Foo. public bool Foo() { using (IMyDisposableClass client = new MyDisposableClass()) { return client.SomeOtherMethod(); } } How can I test something like the code above? Sometimes I choose not to use u...

What should be passed as the objectName when throwing an ObjectDisposedException?

When implementing IDisposable, I undertand that every method that shouldn't be called after the object's been disposed should throw the ObjectDisposedException. But what is the standard for the name object that should be passed to the exception's constructor? ...

How should I ensure disposal of possibly disposable objects?

I am working on a .NET project, which needs to interact with some user defined classes - reffered to as "jobs". All job classes must implement a specific interface IJob in order order for the library to consume them. Sometimes a job class might hold unmanaged resource, which needs to be explicitly disposed. How should I ensure that all ...

Creating an IDisposable class in c# which cleans up an SqlConnection when finished with

In an answer to a previous question somebody recommended: have the SqlConnection a member variable of your class, but make the class IDisposable and dispose of the SqlConnection when the class is disposed I have put together an implementation of this suggestion (below) but wanted to check that this implementation is correct (obviou...

How can release excel object with IDisposeable interface

I am writing an excel class and i want to release this unmanaged object automatically. I'm using IDisposable pattern and write Dispose methods. Example ; class MSExcel : IDisposable { ApplicationClass excel; bool disposed; public MSExcel() { disposed = false; excel = new ApplicationClass(); } ...

How to dispose WCF services correctly?

My WCF service is IDisposable because it uses a ReadWriterLockSlim. When I dispose both of them in the following method: public void Dispose() { lockSlim.Dispose(); } Some of the public methods are still running (or accepting new connections, I don't know), and it fires exceptions because of the attempts of using disposed objects ...