将值参数传递给c#中的任务 [英] Passing value parameter to Task in c#

查看:35
本文介绍了将值参数传递给c#中的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 long 按值传递给任务时遇到问题.

I have an issue with passing a long by value to a Task.

我有一个 ID 列表,我在其中循环遍历每个 ID,分配给一个局部变量,然后作为参数传递给一个新任务.在循环和处理下一个 ID 之前,我不会等待任务完成.我保留了一系列任务,但这无关紧要.

I have a list of ID's where I loop through each one, assign to a local variable then pass as a parameter to a new Task. I do not wait for the task to complete before looping round and processing the next ID. I keep an array of Tasks but this is irrelevant.

loop
    long ID = list[index];
    task[index] = Task.Factory.StartNew(() => doWork(ID));
end loop

如果列表包含例如 100 和 200.我希望第一个任务用 100 调用然后用 200 调用第二个任务.但它没有,doWork 为两个任务接收 200,因此在复制值时会出现问题.

If the list contained for example 100 and 200. I would want the first task called with 100 then the second task called with 200. But it does not, doWork receives 200 for both tasks so there is an issue when the value is copied.

我可以用一些简单的控制台代码来演示

I can demonstrate with some simple console code

class Program
{
   static void Main(string[] args)
   {
      long num = 100;
      Task one = Task.Factory.StartNew(() => doWork(num));
      num = 200;

      Console.ReadKey();
   }

   public static void doWork(long val)
   {
      Console.WriteLine("Method called with {0}", val);
   }
 }

上面的代码会一直显示

使用 200 调用的方法

Method called with 200

我修改了代码等待任务状态从WaitingToRun切换

I modified the code to wait for the task status to switch from WaitingToRun

static void Main(string[] args)
{
   long num = 100;
   Task one = Task.Factory.StartNew(() => doWork(num));
   while(one.Status == TaskStatus.WaitingToRun)
   {}
   num = 200;

   Console.ReadKey();
}

这改进了一些东西,但不是 100% 证明,经过几次运行后,我用 200 调用了 Method

This improves things but not 100% proof, after a few runs I got Method called with 200

还尝试了以下

while (true)
{
    if (one.Status == TaskStatus.Running | one.IsCompleted == true)
    break;
}

但再次显示了 200.

but again got 200 displayed.

有什么想法可以保证在不等待任务完成的情况下传递给任务的值吗?

Any ideas how you can guarantee the value passed to the task without waiting for the task to complete?

感谢任何帮助/建议阿德

Any help/ suggestions appreciated Ade

推荐答案

有什么想法可以保证在不等待任务完成的情况下传递给任务的值吗?

Any ideas how you can guarantee the value passed to the task without waiting for the task to complete?

当然 - 只需创建一个单独的变量,不会在其他任何地方修改.您可以使用新的范围来说明这一点:

Sure - just create a separate variable which isn't modified anywhere else. You can use a new scope to make that clear:

long num = 100;
Task one;
{
    // Nothing can change copyOfNum!
    long copyOfNum = num;
    one = Task.Factory.StartNew(() => doWork(copyOfNum));
}

您不能更改 C# 编译器以捕获创建委托时变量的值"而不是捕获变量,但您可以确保变量之后不会更改,这可以完成相同的事情.

You can't change the C# compiler to capture "the value of the variable when delegate is created" rather than capturing the variable, but you can make sure the variable isn't changed afterwards, which accomplishes the same thing.

这篇关于将值参数传递给c#中的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆