Question

I'm using the ToolStripDropDown to show up an selectionpopup.

The ToolStripDropDown contains a few custom controls with running Threads. The Threads ends with the OnHandleDestroyed event, but for some reason the ToolStripDropDown doesn't dispose/destroy it's handle after closing.

Disposing the ToolStripDropDown on the closed-event gives me an exception because anything still accesses the ToolStripDropDown.

How do I know if the custom Control is still in use or not to end the thread?

Custom-Control:

public class CControl : Control
{
    Thread StyleThread;
    Object lockOBJ = new Object();
    bool abortthread = false;

    public CControl()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.Selectable, false);

        StyleThread = new Thread(new ThreadStart(this.StyleDelegate));
    }


    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        if(!StyleThread.IsAlive)
            StyleThread.Start();
    }

    protected override void OnHandleDestroyed(EventArgs e)
    {
        base.OnHandleDestroyed(e);
        lock (lockOBJ)
        {
            abortthread = true;
        }
        if (StyleThread.IsAlive)
        {
            StyleThread.Join(100);
        }
    }

    ...
}

ToolStripDropDown-Control:

public class AddPopUp : UserControl
{
    /*
        ------------------------------------
        This Control contains some CControls
        ------------------------------------
    */

    public void Show(Control control)
    {
        TSDD tsdd = new TSDD(this);
        Point screenpoint = control.PointToScreen(new Point(0, 0));
        tsdd.Show(control,new Point(0, -tsdd.Height ));
    }

    class TSDD : ToolStripDropDown
    {
        private Control Control { get; set; }

        public TSDD(Control control)
        {
            this.Control = control;
            this.DropShadowEnabled = false;
            ToolStripControlHost ch = new ToolStripControlHost(control);
            this.Items.Add(ch);
        }

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, Control.Size.Width + 16, Control.Size.Height + 18, specified);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                unchecked
                {
                    cp.Style |= (int)0x80000000;
                    cp.Style |= 0x40000;
                }
                return cp;
            }
        }


        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x84)
            {
                m.Result = (IntPtr)1;
                return;
            }
            base.WndProc(ref m);
        }
    }
}

EDIT:

After same more testing i wasn't able to dispose the toolstripdropdown. As a workaround i'm just destroying the handle on close of the toolstripdropdown.

        protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
        {
            base.OnClosed(e);
            this.DestroyHandle();
        }
Was it helpful?

Solution

ToolStrips have a very persistent bug (claimed to be fixed, but I still have problems) where they register an event handler to SystemEvents.UserPreferenceChanged to be able to repaint if the user changes global style settings in the OS.

The only workaround I have found is to remove the attached toolstrips by enumerating the handlers in the SystemEvents.UserPreferenceChanged and removing them upon disposal of the control.

ToolStrip memory leak

http://connect.microsoft.com/VisualStudio/feedback/details/115600/toolstrip-leaks-after-calling-dispose#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top