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();
}
}