Windows Forms: how to do automatic disposing? Here is my answer...
-
Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated.
/* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }
-
Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated.
/* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }
-
Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated.
/* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }
Second version:
public static void disposeComponents(Component f, bool disposeForms) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Form && disposeForms == false) continue; if(value is Form && f is Form) { if( ((Form)f).ParentForm == value ) continue; if( ((Form)f).MdiParent == value ) continue; } if(value is IDisposable && value != null) { ((IDisposable)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }
-
-
Second version:
public static void disposeComponents(Component f, bool disposeForms) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Form && disposeForms == false) continue; if(value is Form && f is Form) { if( ((Form)f).ParentForm == value ) continue; if( ((Form)f).MdiParent == value ) continue; } if(value is IDisposable && value != null) { ((IDisposable)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }
I reiterate - if you don't trust the garbage collector, don't use it. You're fighting the framework, and performance can only suffer. Christian Graus - Microsoft MVP - C++
-
I reiterate - if you don't trust the garbage collector, don't use it. You're fighting the framework, and performance can only suffer. Christian Graus - Microsoft MVP - C++
Forget the garbage collector (comment that line). I've posted that snippet to share with the community this idiom of disposing components in C# Windows Forms applications. Hope this clarify the topic of this post. Thanks however for the suggestion (as I have already said, I am new to C#)