C# 任务继续会把引用搞乱

C# 任务继续会把引用搞乱,c#,asynchronous,task,C#,Asynchronous,Task,所以我的一些代码是这样异步调用的 Task.Run(() => DoSomethingAsync()); private void DoSomething2Async() { Something something = new Something(3); Random rand = new Random(); for (int i = 0; i < 3; i++) { something.a = rand.Next(1000);

所以我的一些代码是这样异步调用的

Task.Run(() => DoSomethingAsync());
private void DoSomething2Async()
{
    Something something = new Something(3);
    Random rand = new Random();
    for (int i = 0; i < 3; i++)
    {
        something.a = rand.Next(1000);

        var task = new Task<int>(()
            => Calculate());

        something.ContinueWith((t)
            => CalculateContinuation(something, task.Result));

        task.Start();
    }

    MessageBox.Show("Do Something2 is done");
}
Task.Run(() => RegisterAllUsers());
DoSomethingAsync本身称为DoSomething2Async,如下所示

Task.Run(() => DoSomethingAsync());
private void DoSomething2Async()
{
    Something something = new Something(3);
    Random rand = new Random();
    for (int i = 0; i < 3; i++)
    {
        something.a = rand.Next(1000);

        var task = new Task<int>(()
            => Calculate());

        something.ContinueWith((t)
            => CalculateContinuation(something, task.Result));

        task.Start();
    }

    MessageBox.Show("Do Something2 is done");
}
Task.Run(() => RegisterAllUsers());
这是一门什么的课

class Something : ICloneable
{
    public int a;

    public Something(int aa)
    {
        a = aa;
    }

    public object Clone()
    {
        Something clone = new Something(a);

        return clone;
    }
}
如您所见,
Calculate
被调用了3次,
CalculateContinuation
也将被调用3次,我想传递给CalculateContinuation 2参数,其中一个是调用前配置的对象(在这种情况下,它是某个对象的实例)第二个是
Calculate
方法的结果。现在的问题是,
Calculate
的结果对于每个调用都是不同的(因为它是随机的)而
something.a对于每个调用也应该不同,因为它也是随机的,但是每次命中
CalculateContinuation
时,都会引用在
DoSomething2Async
中循环的最后一次迭代中配置的某个实例。(如果在循环之前命中它,我认为它将引用当时配置的对象)。我的意思是我得到了
messagebox
,其中
Calculate
的结果是不同的,但是
有些东西。我两天来一直在努力解决这个问题,我不知道该怎么办。我试图传递
something
的克隆,我试图向集合中添加
something
s,并在每次迭代中传递最后一次,但没有任何结果。可能有人有这样的问题。在这种情况下,解决方案是什么。提前谢谢

编辑:

人类可读代码

private void RegisterAllUsers()
{
    Person person = new Person(string.Empty);

    for (int i = 0; i < 3; i++)
    {
        person.Name = "Some name"; // different on each iteration

        // create registration task
        var registrationTask = new Task<bool>(()
            => RegisterUser(person));

        // assign continue with, first parameter is person itself and second is the result of RegisterUse
        registrationTask.ContinueWith((task)
            => RegistrationCallback(person, registrationTask.Result));

        registrationTask.Start();
    }
}

private bool RegisterUser(Person person)
{
    // do registration stuff

    return true; // or false if failes 
}

private void RegistrationCallback(Person person, bool succeded)
{
    // when this method executed the reference of person is whatever was set in RegisterAllUsers
    // for the last time, but i want to have reference to the user which was configured on each iteration

    // suppose 1st user is Steve 2nd is Bob and 3rd is Jack
    // when this methid is hit the name of the user is Jack on all 3 callbacks but the succeded parameter
    // is whatever the RegisterUser returned

    // update registered user's status
}

关于您编辑的代码部分:

Person
只有一个实例:
Person
。然后用
i
开始循环。循环为每个
i
填充
person.Name
,同时启动任务。不幸的是,在任务开始之前,foreach循环已经完成,
person
实例只包含最后分配的值。因此,您有三个计划任务,每个任务都有一个
person
实例,该实例只为其
Name
分配了最后一个值,因为
Name
依赖于我从问题描述推断出的
i

要解决此问题,请为每个任务创建
Person
实例,并在循环中使用
j
变量(关闭问题):

for(int i=0;i<3;i++)
{
int j=i;
Person-Person=新的Person(string.Empty);
person.Name=“Some Name”;//这次取决于j而不是i
....

使用
Something
Something 2
您的代码很难理解。请编辑您的问题并使用真名,或者至少使用不太相似的真名。好的,我会尽力等几分钟。开始吧。我做了一些更改以更清楚地说明问题