Race condition during stream start?

I am running the following code to start my threads, but they do not 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 will start just fine (extra delay added by me, pressing F10 to go through the code).

These are the functions in my forms application:

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

Can anybody help me?

+3
source share
7 answers

, . . - , . - , .

. . ( Captured Variables) , .

+2

lambda ( ).

ConfigurationItem foreach . , ().

, :

foreach (ConfigurationItem tmpItem in config.getAllItems())
{
        i++;
        var currentI = i;
        var currentItem = tmpItem;
        var t = new Thread(() => WorkerThread(currentItem, currentI));
        t.Start();
        //return t;
}

ThreadPool.

+2

, : () => WorkerThread(tmpItem, i)

Func<>, , , .NET 2.0. , WorkerThread(). , ( ).

, , ...

:

var t = new Thread(new ParametrizedThreadStart(WorkerThread));
t.Start(new { ConfigurationItem = tmpItem, Index = i } );

[EDIT] . , .

private void startWorkerThreads()
{
    int numThreads = config.getAllItems().Count;
    int i = 0;

    foreach (ConfigurationItem tmpItem in config.getAllItems())
    {
            i++;
            var wt = new WorkerThread(tmpItem, i);
            wt.Start();
            //return t;
    }
}
private class WorkerThread
{
    private ConfigurationItem _cfgItem;
    private int _mul;
    private Thread _thread;
    public WorkerThread(ConfigurationItem cfgItem, int mul) {
        _cfgItem = cfgItem;
        _mul = mul;
    }
    public void Start()
    {
        _thread = new Thread(Run);
        _thread.Start();
    }
    private void Run()
    {
        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();
        });
    }
}
+1

( )? ThreadPool.

0

, , , .

, - ?

0

, - . , :

  • , , :

    this.textBox1.Text + = "Thread" + Thread.Current.Name + "Complete!\r\n";

  • config.getAllItems(), , ()

===========

winforms:

  • , ThreadPool:

    ThreadPool.QueueUserWorkItem(state = > {WorkerThread (tmpItem, i);});

  • , this.BeginInvoke, this.Invoke = > = >
  • Thread.Sleep , : Thread.Sleep(10 * mul * 100);

, .

0

!

I just implemented threadpool, and it worked like a charm - with the added bonus, don't multiply too many threads at once.

I will also look at other solutions, but this time around threadpool it will save me from having to manually check bozos with too many configurations;)

0
source

Source: https://habr.com/ru/post/1737488/


All Articles