Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Threading UI
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 1-Oct-10 15:27pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Cross thread communication is indeed possible.
 
Have a look here[^] and here[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

this code create and add a button either in UI or background thread
 
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();
        }
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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");
     }
 }
  Permalink  
Comments
NandoMan at 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)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 304
2 Richard MacCutchan 225
3 BillWoodruff 169
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 5 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100