Lambda expressions with multithreading in C#

后端 未结 3 1472
心在旅途
心在旅途 2020-12-19 15:19

I am trying to understand why this program doesn\'t work

Expected output: numbers 0-19 in random order What I get when I run: some numbers repeat, sometimes 20 is pr

3条回答
  •  离开以前
    2020-12-19 15:41

    public void processinThreads()
    {
        for (int i = 0; i < 20; i++)
        {
            int local = i;
            Thread t = new Thread(new ThreadStart(()=>DoSomething(local, processCallback)));
            t.Start();
        }
    }
    

    Your problem is related to closure over lambda.

提交回复
热议问题