C#中使用多线程的Lambda表达式

sat*_*tya 4 c# lambda multithreading

我试图理解为什么这个程序不起作用

预期输出:随机顺序的数字0-19运行时得到的结果:重复一些数字,有时打印20.

请帮忙.我在DoSomething()中尝试使用lock(obj),但它没有帮助.

程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
    public delegate void callbackDelegate(int x);
    class Program
    {
        void processCallback(int x)
        {
            Console.WriteLine("IN callback: The value I got is " + x);
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.processinThreads();
            Console.ReadKey();
        }

        public void DoSomething(int x, callbackDelegate callback)
        {
            Thread.Sleep(1000);
            //Console.WriteLine("I just woke up now " + x);
            callback(x);
        }

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

Jak*_*cki 9

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

你的问题与关闭lambda有关.


Pau*_*yng 7

您应该使用TPL,它比手动线程管理更容易和推荐:

Parallel.For(0, 20, x => {
    Thread.Sleep(1000);
    Console.WriteLine("IN callback: The value I got is " + x);
});
Run Code Online (Sandbox Code Playgroud)

这也将阻塞直到循环结束,如果你不希望你可以使用TPL Task,但我肯定会建议避免线程.