Let’s say I have the following code:
public void Inject(Form subform)
{
this.tabControl1.TabPages[1].Controls.Add(subform);
this.Refresh();
}
How can I convert the Controls.Add() call to a thread-safe call, using Control.Invoke?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only way to make
Control.Addthread safe is to make sure it’s called from the UI thread. This also implies that theControlbeing added is usable from the UI thread.Here is a function though which produces a delegate which can add to a
Controlfrom any thread (presuming the addedControlis OK on the UI thread).Then for a given
Controlyou can pass the resulting delegate to any thread.