Nezreli
Nezreli

Reputation: 1288

ToolTip causes cross thread operation?

Help me understand this. I'm debugging some old code. Anyway I have this method:

    private void textBox1_Validated(object sender, EventArgs e)
    {
        toolTip1.SetToolTip(textBox1, "This is a test tooltip");
        toolTip1.Show("This is a test tooltip", this, label3.Location, 2000);
    }

This method works as expected, it shows a tooltip right after successful validation. This all happens on a child form in a MDI application. If I try to close the form afterwards (doesn't matter if tooltop is visible or not) I get this error:

Cross-thread operation not valid: Control 'TestForm' accessed from a thread other than the thread it was created on at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.SetParentHandle(IntPtr value) at System.Windows.Forms.Control.ControlCollection.Remove(Control value)
at System.Windows.Forms.MdiClient.ControlCollection.Remove(Control value) at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.Form.Dispose(Boolean disposing) at VlastitiBackgroundWorker.BazniEkran.Dispose(Boolean disposing) in D:\TFSWorkspace\VlastitiBackgroundWorker\VlastitiBackgroundWorker\BazniEkran.Designer.cs:line 20 at VlastitiBackgroundWorker.DesetSekundi.Dispose(Boolean disposing) in D:\TFSWorkspace\VlastitiBackgroundWorker\VlastitiBackgroundWorker\DesetSekundi.Designer.cs:line 20 at System.ComponentModel.Component.Dispose() at System.Windows.Forms.Form.WmClose(Message& m)

Why? It's reproducible.

Upvotes: 1

Views: 1401

Answers (3)

Nezreli
Nezreli

Reputation: 1288

Upon further investigation I have found that if I disable the TabbedMDIManager third party component from Syncfusion that this error goes away. I use this component to make my MDI child forms look like tabs just like VisualStudio does.

Now I have no idea what is the connection here between that component and this event method but It's obvious I need to do more investigation and probably contact Syncfusion support.

Thanks for your help.

Upvotes: 1

Tausif Alam
Tausif Alam

Reputation: 41

//Use Invoke or BeginInvoke on the control itself for cross threading manipulation..

            private void textBox1_Validated(object sender, EventArgs e)
            {
                toolTip1.Invoke((MethodInvoker)delegate
                {
                    toolTip1.SetToolTip(textBox1, "This is a test tooltip");
                    toolTip1.Show("This is a test tooltip", this, label3.Location, 2000);
                });
            }

Upvotes: 0

Sampath
Sampath

Reputation: 1211

Try this

private void textBox1_Validated(object sender, EventArgs e)
        {
            this.Invoke((MethodInvoker)delegate
            {
                toolTip1.SetToolTip(textBox1, "This is a test tooltip");
                toolTip1.Show("This is a test tooltip", this, label3.Location, 2000);
            });
        }

Upvotes: 1

Related Questions