Click here to Skip to main content
12,559,868 members (49,145 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# windows-forms

I seem to have run in kind of a race condition. This is the very simplified version of the problem. (In reality there are several layers inbetween, the controls are no Buttons, but also inherited from Control etc.) I _need_ to remove and dispose my controls after e.g. a leave event as below.
If I click on button1 I get an ObjectDisposedException, because it needs to finish something. If I click on button2 it obviuously works.
How can I find out, when it is ready to be disposed? Or how can I force a Control to finish all it needs to do before disposing?
A possible solution should work for all inheritances of Control.
(My first maybe naive try was just to stop all messages as in code below. Nor helps an Application.DoEvents())


using System;
using System.Windows.Forms;
namespace Test
    static class Program
        static void Main()
            Application.Run(new TestForm());
        public class TestForm : Form
            myButton button1;
            myButton button2;
            public TestForm()
                TextBox tb = new TextBox();

                button1 = new myButton();
                button1.Text = "button1";
                button1.Location = new System.Drawing.Point(tb.Right, 0);
                button2 = new myButton();
                button2.Text = "button2";
                button2.Location = new System.Drawing.Point(button1.Right, 0);
                tb.Leave += new EventHandler(tb_Leave);
            void tb_Leave(object sender, EventArgs e)
                button1.WillBeDisposed = true;
                //this will throw an ObjectDisposedException if the Leave event was caused by clicking on button1
                //this will NOT throw an ObjectDisposedException if the Leave event was caused by clicking on button2

                //How can I find out at which point of time the button (or any other Control) can be safely disposed, or how can I force button1 to finish all it needs to do?

        public class myButton : Button
            internal bool WillBeDisposed = false;
            protected override void WndProc(ref Message m)
                if (WillBeDisposed) return; //doesn't help
                base.WndProc(ref m);
Posted 20-Nov-12 2:34am
Updated 20-Nov-12 3:08am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you really have to:

1. Explicitly implement IDisposable in class myButton.
2. If the parent class implements Dispose(), make sure you call the parent version in the first line of your function.
3. Don't forget to call GC.SuppressDispose() (or similar, I don't remember the exact name); google for 'IDisposable implementation'.

But you don't really have to.
You only have to make disposable objects if they contain resources (database connections, sockets, file handles, GDI handles, ...).
Forms and controls are not supposed to manage resources: that's what the MVC pattern (and all those derived from it) is all about. Only classes in the Model should manage resources.

Hope this helps,

vmp5 21-Nov-12 8:06am
Thanks. I'd still be interested in what's happening in the Control.Dispose() implementation and what it tries to access. But you're right, the dispose was a bit misused in my original case, so I could workaround it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161026.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100