我需要并行运行任务并向它们传输大约 3-5 个参数,但现在我向任务传输了 2 个参数,因此,我总是在控制台中看到值 100。
告诉我我做错了什么?以及如何正确地将参数传递给任务?
class Program
{
static void Main(string[] args)
{
/// Init
string file_name = "unknown.dat";
Action<string, int> action = (msg, count) => Load(msg, count);
/// For
for (int i = 0; i < 100; i++)
{
Task.Factory.StartNew(() => action(file_name, i));
}
/// End
Console.Read();
}
public static void Load(string aFileName, int aCount)
{
Console.WriteLine("Index: {0} ", aCount);
}
}
Run Code Online (Sandbox Code Playgroud)