propertygrid throwing error "Controls created on one thread cannot be parented to a control on a dif

J

jaire

In my application I have two threads.
Each of them creates a new propertygrid.
In the property grid I have a property with a color editor.

Whenever i am clicking on the combo box to change the 'Color' in the
first thread is fine.

Any other thread that tries to modify the color in the property grid
throws:
"Controls created on one thread cannot be parented to a control on a
diffrent thread."

Is property grid thread safe? Is there any wrong static variable that
should be threadstatic? Any solution?

Thanks,

jaire
 
J

Jon Skeet [C# MVP]

jaire said:
In my application I have two threads.
Each of them creates a new propertygrid.
In the property grid I have a property with a color editor.

Whenever i am clicking on the combo box to change the 'Color' in the
first thread is fine.

Any other thread that tries to modify the color in the property grid
throws:
"Controls created on one thread cannot be parented to a control on a
diffrent thread."

Is property grid thread safe? Is there any wrong static variable that
should be threadstatic? Any solution?

Any one form should only have controls created on one thread. You
should only ever access those controls from that thread, apart from
calling Control.Invoke, Control.BeginInvoke, Control.CreateGraphics and
Control.InvokeRequired.

See
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
 
J

Jesus Arevalo

Thanks for your quick reply but I think I was not clear:

My application creates two threads.
Each thread creates a form.
Each form creates a property grid.
The color editor is only accessed from his respective thread.

The problem looks like there is something cached on the ColorUI.
Although each property grid is created in a different thread, the
ColorEditor created by reflection ([Editor(typeof(ColorEditor),
typeof(System.Drawing.Design.UITypeEditor))])
is always the same.

Thanks.
 
J

Jon Skeet [C# MVP]

Jesus Arevalo said:
Thanks for your quick reply but I think I was not clear:

My application creates two threads.
Each thread creates a form.
Each form creates a property grid.
The color editor is only accessed from his respective thread.

The problem looks like there is something cached on the ColorUI.
Although each property grid is created in a different thread, the
ColorEditor created by reflection ([Editor(typeof(ColorEditor),
typeof(System.Drawing.Design.UITypeEditor))])
is always the same.

Hmm, possibly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jesus Arevalo

Example:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Data;

namespace ExamplePropertyGrid
{
public class Myobject
{
Color m_color;

public Myobject ()
{
m_color = Color.Black;
}

public Color MyColor
{
get
{
return m_color;
}
set
{
m_color = value;
}
}
}

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

Myobject myobject = new Myobject();
propertyGrid1.SelectedObject = myobject;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(292, 273);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor =
System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Thread thread1 = new Thread(new ThreadStart(NewMain));
// start study in new thread so that database connection is unique.
thread1.Start();

Thread thread2 = new Thread(new ThreadStart(NewMain));
// start study in new thread so that database connection is unique.
thread2.Start();
}

internal static void NewMain()
{
Application.Run(new Form1());
}
}
}
 
W

Willy Denoyette [MVP]

Make sure your second thread runs his own message pump and is initialized
for STA.

Willy.
 
J

Jon Skeet [C# MVP]

Jesus Arevalo said:

<snip>

Thanks. It certainly shows the problem up - and adding [STAThread] to
NewMain doesn't fix it either. I'll investigate and see if I can come
up with anything...
 
W

Willy Denoyette [MVP]

Not sure why you need the same property browser object (Color) in two
separate threads of one application?
Or the color picker object was not designed to be used in such scenario, or
it's a bug.
There is no such problem if the objects are different components (f.i Color
and Font).
What exactly are you trying to achieve?

Willy.

Jesus Arevalo said:
Example:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Data;

namespace ExamplePropertyGrid
{
public class Myobject
{
Color m_color;

public Myobject ()
{
m_color = Color.Black;
}

public Color MyColor
{
get
{
return m_color;
}
set
{
m_color = value;
}
}
}

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

Myobject myobject = new Myobject();
propertyGrid1.SelectedObject = myobject;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(292, 273);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor =
System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Thread thread1 = new Thread(new ThreadStart(NewMain));
// start study in new thread so that database connection is unique.
thread1.Start();

Thread thread2 = new Thread(new ThreadStart(NewMain));
// start study in new thread so that database connection is unique.
thread2.Start();
}

internal static void NewMain()
{
Application.Run(new Form1());
}
}
}
 
J

Jesus Arevalo

I have just created this piece of code to reflect my problem. My
application is a little bit more complex ;), where I can open like
multiple documents and each document needs to be opened in a separate
thread. Each document contains plots and the plots are controlled by
property grids to modify some properties.

..net gurus I need your help!!!
 
J

Jon Skeet [C# MVP]

Jesus Arevalo said:
I have just created this piece of code to reflect my problem. My
application is a little bit more complex ;), where I can open like
multiple documents and each document needs to be opened in a separate
thread. Each document contains plots and the plots are controlled by
property grids to modify some properties.

Out of interest, why do you particularly *need* each document to be in
a separate thread? Most applications keep all UI stuff in a single
thread, which would get rid of your problem, I'm sure. I still don't
know why it's failing, but unless there's a compelling reason for
keeping different documents in different threads, I'd consider
redesigning.
 
W

Willy Denoyette [MVP]

Ok, we know there is a problem with the color picker control, but can you
tell us what control(s) you have on your grid, and what control throws the
error?
Also I'm still not clear why you need two UI threads, even if you like to
open each document in his own thread, you should stick with one single UI
thread.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top