Create an user-control from another thread
I wish to create a button ( made by me as a user-control ) from another thread other than the one i wish to create on . The thing is that i know how to modify a user-control object from another thread ( using a delegate and .InvokeRequired ) 开发者_高级运维but i don't know how i could create one . Any Suggestions ?
Try the following:
// From the other thread
userControl11.BeginInvoke(new Action(() =>
{
var button = new Button();
button.Text = "My new button";
userControl11.Controls.Add(button);
}));
Within the delegate (the () => { }
), you can do anything you like with the user control and the form.
Please read my previous post:
why isn't user-control class access not safe from another thread?
Control Threading:
http://msdn.microsoft.com/en-us/library/ms171728.aspx
Use the same way as for modifying a user control. Function that creates a control must be executed in UI thread, and called using Invoke or BeginInvoke. Pass all information required for control creation using delegate parameters.
精彩评论