Click here to Skip to main content
11,492,523 members (62,111 online)
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 Sergey Alexandrovich Kryukov 399
1 Maciej Los 266
2 KarstenK 210
3 CPallini 208
4 Sascha Lefèvre 195
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 5 Oct 2010
Copyright © CodeProject, 1999-2015
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