By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,245 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,245 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
jaire <ja*****@eresmas.com> wrote:
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/m...#windows.forms

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

P: n/a
Jesus Arevalo <ja*****@eresmas.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
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());
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

P: n/a
Make sure your second thread runs his own message pump and is initialized
for STA.

Willy.

"Jesus Arevalo" <ja*****@eresmas.com> wrote in message
news:ua**************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6

P: n/a
Jesus Arevalo <ja*****@eresmas.com> wrote:
Example:


<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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
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" <ja*****@eresmas.com> wrote in message
news:u2**************@TK2MSFTNGP09.phx.gbl...
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());
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8

P: n/a
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!!!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

P: n/a
Jesus Arevalo <ja*****@eresmas.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

P: n/a
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.

"Jesus Arevalo" <ja*****@eresmas.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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!!!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.