Introduction
Well, if you are working with threads and want to access a control that is created in another thread then you will always face a problem saying that

To solve the tiny problem, Here is the solution
First of all, start the thread and assign it a method you want to execute it on.
Remember, the method you want to assign to the thread should not contain any parameters.
thread = new Thread (new ThreadStart (RunLoop));
thread.Start();
Now here is the RunLoop method
private void RunLoop(int i)
{
if (listBox1.InvokeRequired)
{
// If Invoke is required, call the method recursively using Invoke
MethodInvoker AssignMethodToControl = new MethodInvoker(() => RunLoop(i));
listBox1.Invoke(AssignMethodToControl);
}
else
{
// If Invoke is not required, execute the loop and add items to the listBox
for (int j = 0; j < 1000; j++)
{
listBox1.Items.Add(j);
}
listBox1.Items.Add(i);
}
}
In the first line, we get to know whether the control we want to access is created on the same thread or another thread. If it's created on another thread, then it will execute the second line.
In the second line, we create a MethodInvoker with the name AssignMethodToControl, which is a delegate that executes a method that has no parameters.
In the third line, we invoke the MethodInvoker to the Control.
We have achieved our aim, so the method will re-execute, and it will loop through the condition.


Fernando SalasPosted Nov 29, 2019, 12:03 AM
It can be possible to make it more complex ?
Fernando SalasPosted Nov 29, 2019, 12:02 AM
Excelent program.
Hanif HefazPosted Jul 21, 2016, 9:34 AM
Great
Mohammad Ajmal AmirzadPosted Jul 14, 2011, 9:14 AM
Well. Before posting this article, i was hesitant that whether you all will like my articles or not. But yeah, i have to say that you all have given me a great support. Inshallah i will begin to write regularly for MindCracker Community.
Dea SaddlerPosted Jul 14, 2011, 7:32 AM
Great Article
Rohatash KumarPosted Jul 14, 2011, 7:32 AM
Nice article. Ajmal
Manish TewatiaPosted Jul 14, 2011, 7:22 AM
Welcome Ajmal Good Start.... Nice Article
Dinesh BeniwaleditedPosted Jul 14, 2011, 6:40 AMEdited Jul 14, 2011, 6:40 AM
Very useful post, Welcome to the Mindcracker Community.