dispose

Is it considered acceptable to not call Dispose() on a TPL Task object?

I want to trigger a task to run on a background thread. I don't want to wait on the tasks completion. In .net 3.5 I would have done this: ThreadPool.QueueUserWorkItem(d => { DoSomething(); }); In .net 4 the TPL is the suggested way. The common pattern I have seen recommended is: Task.Factory.StartNew(() => { DoSomething(); }); How...

With a windows Application (winapp) when/where should i Dispose of my UnitOfWork?

Hi folks, I've got a windows application that creates some threads to parse some txt files. Nothing too crazy. I'm using StructureMap Dependency Injection to create any instances of my Services and Repositories. So my Winform has a static property which returns the instance, based on what's been registered. Works great .. except I'm n...

How to dispose of resources in iTextSharp append method on exception

I have the following method using the iTextSharp library which should process and move the source document to a target document (which may or may not already exist). There is an append flag which will append the contents if the document already exists. I'm throwing an exception if append isn't set, but I'm having some problems working o...

System.InvalidOperationException: Value Dispose() cannot be called while doing CreateHandle()

In my windows forms app clients sometimes report a strange exception: System.InvalidOperationException: Value Dispose() cannot be called while doing CreateHandle() at System.Windows.Forms.Control.Dispose(Boolean disposing) at System.Windows.Forms.ContainerControl.Dispose(Boolean disposing) at System.ComponentModel.Component.Dis...

Object disposal inside the thread

I'm programming a plug-in for a large system. The systems runs my plug-in in an independent thread. Inside the plug-in I'm allocating an unmanaged resource. I need to be 100% sure I release this driver. I implemented the IDisposable pattern and I covered all methods the system claims to call if the plug-in is about to be terminated. ...

Specific questions about C# Dispose Pattern

I have a few basic questions about the Dispose Pattern in C#. In the following code snippet, which seems to be a standard way of implementing the dispose pattern, you’ll notice that managed resources are not handled if disposing is false. How/when are they handled? Does the GC come along and handle the managed resources later? But if...

If the autoreset is set to false, will my timer be disposed automaticaly ?

I launch a timer only one time in my application : CustomTimer timer = new CustomTimer(mod); timer.Interval = interval.TotalMilliseconds; timer.AutoReset = false; timer.Start(); So the AutoReset is set to false. At the end of the timer, will the dispose method be called automatically ? ...

Dispose of AddIns created using MAF (System.AddIn)

Hi all, Does anyone know how to dispose of AddIns created using System.AddIn. All the examples online seem to show how to easily load and use an addin, but none show how to dispose of them once they're alive. My Problem is I create addins in new processes, and these processes never get garbage collected, obviously a problem. Below is ...

Finalizer and IDisposable

Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up? As I see it, if the ...

Automatic Disposal Extension Method reasonable?

I've been writing some custom WinForm controls which do a pretty heavy amount of drawing and therefore tend to have a lot of disposable graphics based fields lying around (Brushes, Pens, Bitmaps etc..) and as a result my Control's Dispose() method has to call Dispose on each of them. I got concerned that I (or a future maintainer) could...

Should I reuse a FileStream/BinaryWriter object?

Update: After looking in the event log at around the time this occurred, I get the message: "The server was unable to allocate from the system nonpaged pool because the pool was empty." repeated continually throughout the log, until it was rebooted. I am writing a class that writes debugging information to a file, up until now the class...

C# disposing objects

I know the Dispose() method is called on the StreamReader object when you have the following code: //Sample 1 using (StreamReader sr1 = new StreamReader(@"C:\Data.txt")) { string s1 = sr1.ReadToEnd(); //Do something with s1... } But if you write the code like this (Sample 2) will the Dispose() method get called too? //Sample ...

Singleton object disposal

I've been doing some playing around with POCO objects and EntityFramework. Because of this I have to write my own Context and Repository(s). I would like all repositories to use the same context instance so changes are shared between them. To facilitate this I coded my Context as a Singleton. This way instead of getting a new context and...

Entity framework: detaching but keeping object graph

Hi, I'm working in a disconnected scenario, but I noticed that disposing an object context does not release attached entities. As a result, subsequent operations often fail because of this. So to solve this, I detach everything myself when the object context is being disposed: public void Dispose() { // detaching is not really need...

"Object can be disposed of more than once" error

When I run code analysis on the following chunk of code I get this message: Object 'stream' can be disposed more than once in method 'upload.Page_Load(object, EventArgs)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object. using(var stream = File.Open(newFilename, FileMode....

When to dispose and why?

I asked a question about this method: // Save an object out to the disk public static void SerializeObject<T>(this T toSerialize, String filename) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); TextWriter textWriter = new StreamWriter(filename); xmlSerializer.Serialize(textWriter, toSerialize); ...

Is it ok not to dispose a MemoryStream / StringReader?

I would like to create a method that returns an XmlReader. Depending on the circumstances the XmlReader may be fed different types of streams, either a StringReader or a MemoryStream. Normally I dispose a StringReader or MemoryStream with a using block, but since I want to return an XmlReader instead, I cannot do that if I want to go wi...