dispose

What best practices for cleaning up event handler references?

纵饮孤独 提交于 2019-11-28 16:42:28
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.SomeUnitsFailed -= Session_SomeUnitsFailed; Session.UnitCheckedIn -= Session_UnitCheckedIn; UnattachListeners(); } The purpose being to clean up all event subscriptions that we have registered for on the target (Session) so that Session is free to be disposed by the GC. I had a discussion with a co-worker about classes that implement

Why call dispose(false) in the destructor?

廉价感情. 提交于 2019-11-28 15:20:53
What follows is a typical dispose pattern example: public bool IsDisposed { get; private set; } #region IDisposable Members public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { //perform cleanup here } IsDisposed = true; } } ~MyObject() { Dispose(false); } I understand what dispose does, but what I don't understand is why you would want to call dispose(false) in the destructor? If you look at the definition it would do absolutely nothing, so why would anyone write code like this? Wouldn't it

Timer not disposed when form is

久未见 提交于 2019-11-28 11:14:46
I am trying to understand why a Windows.Forms.Timer is not disposed when the form that created it is. I have this simple form: public partial class Form1 : Form { private System.Windows.Forms.Timer timer; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer = new Timer(); timer.Interval = 1000; timer.Tick += new EventHandler(OnTimer); timer.Enabled = true; } private void OnTimer(Object source, EventArgs e) { Debug.WriteLine("OnTimer entered"); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { this.Dispose(); } } When I

Should I dispose a Mutex?

痞子三分冷 提交于 2019-11-28 10:58:23
I'm working on 2 Windows Services that have a common database which I want to lock (cross-process) with a system Mutex. Now I'm wondering whether it's ok to just call WaitOne() and ReleaseMutex() in a try-finally block or should I also dispose the Mutex (e.g. in a using block). If so I guess I should always catch the AbandonedMutexException on the WaitOne() method or am I wrong here? A mutex is a Windows kernel object (here wrapped in a .NET object). As such, it is an unmanaged resource that should be disposed. More accurately, the .NET object contains a HANDLE to the mutex, which must be

When do we need to call Dispose() in dot net c#?

回眸只為那壹抹淺笑 提交于 2019-11-28 10:12:00
Do I need to dispose a sqldatareader after it is created? SqlDataReader reader; --- --- --- reader.Close(); reader.Dispose(); Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown: using (var reader = conn.ExecuteReader()) { ... } Object which are Disposable can be best used (if possible) in a using block. At the end of the using block, the object is automatically disposed. Because of

To Dispose or not to Dispose (CA2000)

寵の児 提交于 2019-11-28 09:54:27
I'm switching on Code Analysis on an older project. Most remarks that result I can understand, but the CA2000: Dispose objects before losing scope is hard to get right. For instance, this code from an ASP.Net page: private void BuildTable() { HtmlTableRow tr = new HtmlTableRow(); HtmlTableCell td = new HtmlTableCell(); tr.Cells.Add(td); // add some controls to 'td' theTable.Rows.Insert(0, tr); // 'theTable' is an HtmlTable control on the page } Gives CA messages: CA2000 : Microsoft.Reliability : In method 'BuildTable()', call System.IDisposable.Dispose on object 'tr' before all references to

How and when are c# Static members disposed?

一笑奈何 提交于 2019-11-28 08:54:49
I have a class with extensive static members, some of which keep references to managed and unmanaged objects. For instance, the static constructor is called as soon as the Type is referenced, which causes my class to spin up a blockingQueue of Tasks. This happens when one of the static methods is called, for example. I implemented IDisposable, which gives me methods to handle disposal on any instance objects I created. However, these methods are never called if the consumer doesn't create any instance objects from my class. How and where do I put code to dispose of references maintained by the

.NET object events and dispose / GC

帅比萌擦擦* 提交于 2019-11-28 08:40:27
EDIT: After Joel Coehoorns excellent answer, I understand that I need to be more specific, so I modified my code to be closer to thing I'm trying to understand... Events: As I understand, in the background the events are "collection" of EventHandlers aka Delegates which will be executed when event raised. So for me it means that if object Y has event E and object X subscribes to event Y.E , then Y will have reference to X, since Y must execute the method located in X, in that way, X can not be collected , and that thing i understand. //Creates reference to this (b) in a. a.EventHappened += new

Why “Finalize method should not reference any other objects”?

岁酱吖の 提交于 2019-11-28 07:33:37
问题 I have been pondering why it is recommended that we should not release managed resources inside finalize. If you see the code example at http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx , and search for string "Dispose(bool disposing) executes in two distinct scenarios" and read that comment, you will understand what I mean. Only possibility I can think of is that it probably has something to do with the fact that it is not possible to predict when finalizer will get

Exception: Parameter is not valid (on passing new image to pictureBox)

妖精的绣舞 提交于 2019-11-28 07:25:46
问题 I already had an image inside PictureBox control, and now I want to pass a new one to it. What happens, is that allpication Disposes (and I catch an exception: "Parameter is not valid"). This is my code: using (Image img = Image.FromFile(open.FileName)) { part.Picture = img; pictureBox1.InitialImage = null; pictureBox1.Image = img; } So when the code goes out of the method, it goes streight to Displose of this and main form. I catch the exception only on line where Form1 was started. On this