472,371 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Adding Controls within a Thread?

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
2 3553
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sue | last post by:
On code-behind page: (attributes set programatically for each of these elements) linkbutton added to tablecell textbox added to tablecell tablecells added to tablerow tablerow added to table...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Arthur Dzhelali | last post by:
I am trying dynamically create controls and add them to the form on the Separate thread. I know that I have to use delegate in order to update preexisting controls on the form, but when I ma...
2
by: Olaf Rabbachin | last post by:
Hi folks, I have a form in which I'm starting a separate thread (which is searching directories for image-files and generating thumbnails of them in different dir). Within the form, I also have...
3
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
2
by: _MC_ | last post by:
Hi, i want to structure two elements (each conists of 1 Label and 1 Textbox) in an Table. As I use an Content Place Holder, i thought it is possible to add the table via Controls.add(Literal)....
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
1
by: NickP | last post by:
Hi there, I've made a few animated controls but would like to check on the correct procedure for doing so as unfortunately every now and then, without warning, I recieve a GDI+ Exception stating...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.