dispose

Should I implement my own connection pooling scheme?

Should I write my own connection pooling scheme? (Question rewritten again so I can upvote peoples answers) ...

Invoking Something Twice Leads To: "protected override void Dispose"

I have a function that helps me close forms without getting crossthread errors: public void OutsideClose(long Id) { MessageBox.Show(""); if (InvokeRequired) { Invoke(new Action<long>(OutsideClose), Id); } else { var asdf = ListForm.Find(a => a.Id == Id); ...

Disposing 2nd Time Doesn't Dispose Form?

I use this method to close forms according to their Id, but if I open a form with Id 2, close it once, it works fine, but if I open a form again with Id 2, it doesn't close the form (the form just stays on the screen). I have no idea what is going on and desperately need help. Thanks! public void OutsideClose(long Id) { if (Invoke...

What happens if i return before the end of using statement? Will the dispose be called?

I've the following code using(MemoryStream ms = new MemoryStream()) { //code return 0; } The dispose() method is called at the end of using statement braces } right? Since I return before the end of the using statement, will the MemoryStream object be disposed properly? What happens here? ...

What best practices for cleaning up event handler references?

Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses(); Session.AllUnitsReady -= Session_AllUnitsReady; Session.AllUnitsResultsPublished -= Session_AllUnitsResultsPublished; Session.UnitFailed -= Session_UnitFailed; Session...

Should Dispose methods be unit tested?

I am using C#. Is it advised to unit test dispose methods? If so why, and how should one test these methods? ...

How to dispose a list of disposable objects ?

I am implementing IDisposable for a class and while disposing there is a internal list of disposable objects. Should I be disposing those object by looping through them. public Class MyDisposable Implements IDisposable private _disposbaleObjects as new List(of OtherDisposables) Public Overloads Sub Dispose() Implements...

coding this function without memory leak! - please advise

Hello, In the following code example,will filestream and streamreader get disposed or will they create memory leaks? Is it possible to code this function without causing memory leaks? string ReadFile(string strPath) { using (FileStream fstream = new FileStream(strPath, FileMode.Open)) { using (Stre...

Disposing an Object after a (possibly redirected) request

I have a CustomRequestContext object that has to be disposed after each request. I create it in Page_Load and dispose of it in Page_Unload. The only issue is that in certain circumstances I need to call Server.Transfer to redirect to another aspx page instead. In this case, the object should not be unloaded until the new page is ready to...

Dispose a stream in a BizTalk pipeline component?

I'm fairly new to BizTalk and creating a custom pipeline component. I have seen code in examples that are similar to the following: public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg) { Stream originalDataStream = pInMsg.BodyPart.GetOriginalDataStream(); StreamReader strReader = new StreamReader(originalDat...

How can I get a class to "reset"?

I am writing a C#.NET application. I have a form. When the form is created I create an instance of a class. When I close the form, I want to dispose of the class so that the next time I open the form I can just create a fresh new instance of the class. So, in the form_Closing event I added code like this: classInstance = null; The proble...

Dispose on nested Disposable items?

I wanted to know if there are any conventions regarding disposal of disposable items nested inside another disposable item(in a property/public field, not as private members). For example, a DataSet contains DataTable(s) and a SqlCommand contains a SqlConnection. The obvious thing would be for a class to dispose of all Disposable items ...

Widget is disposed

Good Mourning everybody I have a main screen, with follows keybindings: shell.getDisplay().addFilter(SWT.KeyUp, new Listener() { @Override public void handleEvent(Event event) { switch (event.keyCode) { case SWT.ESC: shell.close(); break; case SWT.F12: display.syncExec...

Is there any way to reduce the amount of boilerplate code for IDisposable?

My project has many reader and writer classes. I started implementing IDisposable, but I believe that it adds many boilerplate code to my classes. For each of the classes, I need to implement: A destructor. A Dispose() method. A Dispose(bool disposing) method. A "bool disposed" field. A check to see if the object is already disposed on...

C#: Properly disposing C# objects when created via COM Interop from VB6.

Im writing a C# class library component which is going to act as a TCP server. It will listen for and receive XML via a specific port, deserialize it and then raise events containing the resulting object as an event argument. The class library itself will be consumed by a VB6 application which will receive and handle the events and asso...

still trying to understand the dispose pattern

hello i've read msdn and various posts about dispose pattern, and there are still a couple of things i don't understand. I've written the following code to test the dispose pattern. Please note that there aren't unmanged resources, i'm using vs2008 and .net 3.5 : public partial class Form1 : Form { public Form1() { Ini...

Cannot access a disposed object

I am writing a tool to integrate with a web service, I have a method which just builds an ImportExportSoapClient object which is used to call the API methods for the web service, but when I call one of the methods I am getting Cannot access a disposed object System.ServiceModel.Channels.ServiceChannel? anyone had a similar experience or ...

How to check if object has been disposed in C#

Is there a method to check if object has been disposed different then try { myObj.CallRandomMethod(); } catch (ObjectDisposedException e) { // now I know object has been disposed } In my case I'm using TcpClient class that has Close() method which disposes object and this can happen in piece of code I don't have control of. In...

Using the "Using" Statement with Typed Dataset Table Adapters

Simple Question. Is it important to use the using statement when working with typed dataset table adapters? Most examples I have seen on the net never use the using statement. using (Data_ItemTableAdapter adapter = new Data_ItemTableAdapter()) { DataItemTable = a.GetDataByDataItemID(DataItemID); // more code here } Thanks...

Disposing of static resources in web service

In a pre-WCF .NET (C#) web service, I have an expensive IDisposable resource that I'm holding a static (actually ThreadStatic) reference to. (Internally it holds a SqlConnection.) How can I insure that this is disposed when the app pool refreshes, should I simply suppress the FxCop warning and not worry about it, or is there a third opti...