简体   繁体   English

为单个任务创建多个线程

[英]Multiple threads created for single Task

I helped create a background task system for an ASP.NET web site. 我帮助为ASP.NET网站创建了后台任务系统。

This is my root Task 这是我的根本Task

Task.Factory.RunNew(RunTimer);

This is called from the root Task . 这是从根Task调用的。

private void RunTimer() 
{ 
    while (!cancellationToken.IsCancellationRequested) 
    { 
        var backgroundTasks = _tasks.Values.ToArray(); 
        var tplTasks = new List<Task>(); 
        foreach (var backgroundTask in backgroundTasks) 
        { 
            var newTask = new Task(() => backgroundTask.Run()); 
            tplTasks.Add(newTask); 
            newTask.Start(); 
        } 
        Task.WaitAll(tplTasks.ToArray()); 
        for (int i = 0; i < NumberOfSecondsToWait && 
            !cancellationToken.IsCancellationRequested; i++)
        { 
            Thread.Sleep(new TimeSpan(0, 0, 1)); 
        } 
    } 
}

_tasks is a ConcurrentDictionary<string, IBackgroundTask> . _tasksConcurrentDictionary<string, IBackgroundTask> For what ever reason, newTask is executed 2 times on separate threads -- namely backgroundTask.Run() is called twice. 无论出于何种原因, newTask在单独的线程上执行两次-即backgroundTask.Run()被调用两次。 RunTimer is only called once. RunTimer仅被调用一次。 NumberOfSecondsToWait is 60. I've verified that tplTasks only has 2 items in it. NumberOfSecondsToWait是60。我已经验证tplTasks只有2个项目。

Anyone have any idea? 有人知道吗

This is because lambdas (in particular, the newTask lambda) bind to variables, not values. 这是因为lambda(尤其是newTask lambda)绑定到变量,而不是值。

You need: 你需要:

...
foreach (var backgroundTask in backgroundTasks)
{
  var localBackgroundTask = backgroundTask;
  var newTask = new Task(() => localBackgroundTask.Run());
  ...
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM