I'm running the following code to start my threads, but they don't start as intended. For some reason, some of the threads start with the same objects (and some don't even start). If I try to debug, they start just fine (extra delay added by me clicking F10 to step through the code).
These are the functions in my forms app:
private void startWorkerThreads()
{
int numThreads = config.getAllItems().Count;
int i = 0;
foreach (ConfigurationItem tmpItem in config.getAllItems())
{
i++;
var t = new Thread(() => WorkerThread(tmpItem, i));
t.Start();
//return t;
}
}
private void WorkerThread(ConfigurationItem cfgItem, int mul)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(10*mul);
}
this.Invoke((ThreadStart)delegate()
{
this.textBox1.Text += "Thread " + cfgItem.name + " Complete!\r\n";
this.textBox1.SelectionStart = textBox1.Text.Length;
this.textBox1.ScrollToCaret();
});
}
Anyone able to help me out?
The problem seems to be there :
() => WorkerThread(tmpItem, i)
I'm not used to
Func<>
but it seems to work like anonymous delegates in .NET 2.0. Thus, you may have a reference to the arguments of theWorkerThread()
method. Hence, their values are retrieved later (when the thread actually runs).In this case, you may already be at the next iteration of your main thread...
Try this instead :
[EDIT] Other implementation. More flexible if you need to pass new parameters to the thread in the future.