Click here to Skip to main content
15,886,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello colleagues

Is it possible to create a control in the non UI thread and then use it with controls created in the UI thread? How?

Thanks and happy weekend!
Posted

Cross thread communication is indeed possible.

Have a look here[^] and here[^].
 
Share this answer
 
this code create and add a button either in UI or background thread

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void CreatingControl()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new d(this.CreatingControl));
            }
            else {
                Button b = new Button();
                this.Controls.Add(b);
            }
        }
        public delegate void d();

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new                    ThreadStart(this.CreatingControl));
            t.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.CreatingControl();
        }
    }
 
Share this answer
 
I'm sorry I didn't made myself clear.

The question is:

If I create a control in a non UI thread, can I add it to the child controls of a control created in the UI thread?
 
Share this answer
 
Ok, I know that I can marshal the creation of the control to the UI thread. But for various circumstances I will not post here, the creation of the control is done in the non UI thread. So, without marshalling the creation of the control, can I add that control created in the non UI thread as a child control of another control created in the UI thread?

Thanks!
 
Share this answer
 
C#
public partial class Form1 : Form
 {
     public Form1()
     {
         InitializeComponent();
     }
     public void AddControlToTheForm(Control controlToAdd)
     {
         if (this.InvokeRequired)
         {
             this.Invoke(new d(this.AddControlToTheForm),new object [] {controlToAdd});
         }
         else {
             this.Controls.Add(controlToAdd);
         }
     }
     public delegate void d(Control c);
     void BackgroundControlConstructor(object whatIsit)
     {
         Button b = new Button();
         b.Text = "I have been created in background";
         //we transfered an object with instructions
         if ((string)whatIsit == "BigButton")
             b.Width = 200;
         //you can see that whill the thread is creating a control UI works
         Thread.Sleep(2000);
         this.AddControlToTheForm(b);
     }

     private void button1_Click(object sender, EventArgs e)
     {
         Thread t = new Thread(new ParameterizedThreadStart(this.BackgroundControlConstructor));
         t.Start("BigButton");
     }
 }
 
Share this answer
 
Comments
NandoMan 5-Oct-10 22:00pm    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900