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

Adding Controls within a Thread?

P: n/a
Hello,
I am writing a program in Visual C# and I have encountered a problem.
In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling the Controls.Add() method several times or even calling the Controls.AddRange() method once can take a huge amount of time. Therefore, I would like to be able to run the loop that creates the controls on a different thread and meanwhile give the user a progress indicator dialog with an option to abort the operation. The probelm is that when I try to use the Controls.Add() method within the new thread I get a runtime error that states: "Additional information: Controls created on one thread cannot be parented to a control on a different thread." What should I do?
Thanks for your help,
Aviv.

Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The user interface is not thread-safe, so unfortunately you cannot perform
any operations on the user interface from other threads.

Are you running SuspendLayout() before you run all these control Add
functions?

Out of interest, why are you adding thousands of controls to a form? That
sounds kinda questionable.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"avivgur" <av*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
Hello,
I am writing a program in Visual C# and I have encountered a problem.
In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling the Controls.Add()
method several times or even calling the Controls.AddRange() method once can
take a huge amount of time. Therefore, I would like to be able to run the
loop that creates the controls on a different thread and meanwhile give the
user a progress indicator dialog with an option to abort the operation. The
probelm is that when I try to use the Controls.Add() method within the new
thread I get a runtime error that states: "Additional information: Controls
created on one thread cannot be parented to a control on a different
thread." What should I do? Thanks for your help,
Aviv.

Nov 16 '05 #2

P: n/a
It sounds like you need to create a single custom control that draws and
manages each tile itself, rather than as separate controls. You have to
understand that each control on the form represents a windows handle and
potentially several resources... if you're going to be putting thousands of
these on a form, then you'd most certainly run out of system resources and
the process will terminate, and on some operating systems the entire
computer will become unstable.

So why not just paint the tiles yourself in a single control? Shouldn't be
that difficult. Plus, if they're rectangular tiles of the same shape then
hit-testing for user interaction is quite simple too.

On the thread issue, I can most certainly tell you that the Windows user
interface is not thread safe. In order to perform operations on the control
it must be invoked in the same thread as the user interface. You've probably
just been lucky so far. Read this for more info:
http://www.yoda.arachsys.com/csharp/...#windows.forms

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"avivgur" <av*****@discussions.microsoft.com> wrote in message
news:B9**********************************@microsof t.com...
Hi,
I'll tell you a little bit about what I'm trying to do:
I want the form to contain a kind of board. This board consists of several several square tiles, each of which I want to be able to manipulate
independently (such as change their color). Also, I would like all the
tiles to have an existance and to repaint themselves properly while the form
is moved in and out of view. I thought about making just one board control
and dividing it into several squares visually, but then it would be hard to
repaint only small sections and manipulate the independent tiles. I tried adding SuspendLayout() and ResumeLayout() before and after the call to Controls.AddRange() but this did not improve the situation. Any ideas?
Aviv

P.S:
Also, I'm not sure that the interface is not thread-safe because I was able to run a similar thread in which I resized all the tiles on the board
on a thread. Just adding controls seems to be troublesome.
"John Wood" wrote:
The user interface is not thread-safe, so unfortunately you cannot perform any operations on the user interface from other threads.

Are you running SuspendLayout() before you run all these control Add
functions?

Out of interest, why are you adding thousands of controls to a form? That sounds kinda questionable.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"avivgur" <av*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
Hello,
I am writing a program in Visual C# and I have encountered a problem.
In my program I want to dynamically create a multitude of controls

(thousands) on a form. The problem is that calling the Controls.Add()
method several times or even calling the Controls.AddRange() method once can take a huge amount of time. Therefore, I would like to be able to run the loop that creates the controls on a different thread and meanwhile give the user a progress indicator dialog with an option to abort the operation. The probelm is that when I try to use the Controls.Add() method within the new thread I get a runtime error that states: "Additional information: Controls created on one thread cannot be parented to a control on a different
thread." What should I do?
Thanks for your help,
Aviv.


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.