Adding Controls within a Thread?

G

Guest

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

John Wood

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 said:
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?
 
J

John Wood

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/multithreading.html#windows.forms

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

avivgur said:
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
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


(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?
[/QUOTE]
 

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