adding controls from another thread

  • Thread starter Thread starter Eddie
  • Start date Start date
E

Eddie

Hi,

I want to write a program that adds a button to the UI every n seconds. I
have a thread written to do this, but when it runs I get a runtime error
saying that a control created in one thread can't be attached to a
container in another thread. Is there a way around this? Is what I want
to do possible?

Thanks,
bye.
 
Hi,

I want to write a program that adds a button to the UI every n seconds. I
have a thread written to do this, but when it runs I get a runtime error
saying that a control created in one thread can't be attached to a
container in another thread. Is there a way around this? Is what I want
to do possible?

Have the code that actually creates/adds the button on the main (UI)
thread and then call it from your other thread. Windows Forms controls
are not thread-safe -- they can only be used from the thread in which
they were created.
 
Hello,

Eddie said:
I want to write a program that adds a button to the
UI every n seconds. I have a thread written to do this,
but when it runs I get a runtime error saying that a
control created in one thread can't be attached to a
container in another thread.

Create the control in the GUI thread. You can create a method in the form
that can be invoked by the other thread which creates the controls.

http://www.devx.com/dotnet/Article/11358
 
Back
Top