C# 基本任务使用

C# 基本任务使用,c#,task,C#,Task,如果播放器的当前位置没有花(“X”),我会尝试放置一个字符串(“-”)。在我放置“-”的同时,我需要开始一项任务。这将每秒钟更改另一个状态的“-”。当我们获得集合的结尾(字符串[])时,任务应该完成。所以每朵花都有一个任务,还有一个CancellationToken(我需要检查是否有取消)。 但当我开始任务时,什么也没有发生 public class Flower { public int FlowerX { get; set; } public int FlowerY { ge

如果播放器的当前位置没有花(“X”),我会尝试放置一个字符串(“-”)。在我放置“-”的同时,我需要开始一项任务。这将每秒钟更改另一个状态的“-”。当我们获得集合的结尾(字符串[])时,任务应该完成。所以每朵花都有一个任务,还有一个CancellationToken(我需要检查是否有取消)。 但当我开始任务时,什么也没有发生

public class Flower
{
    public int FlowerX { get; set; }
    public int FlowerY { get; set; }
    public string[] States { get; set; }
    public string CurrentState { get; set; }
    public Task FlowerTask { get; set; }
    public CancellationToken FlowerCt { get; set; }

    public Flower()
    {

    }
    public Flower(int flowerX, int flowerY, string currentState)
    {
        FlowerX = flowerX;
        FlowerY = flowerY;
        CurrentState = currentState;
    }

    public Flower(int flowerX, int flowerY)
    {
        FlowerX = flowerX;
        FlowerY = flowerY;
        States = new string[] { "-", "+", "w", "W", "o", "O", "X" };
    }
}
public class Garden
{
    public int PlayerX { get; set; }
    public int PlayerY { get; set; }
    public List<Flower> Flowers { get; set; }
    public int Length { get; private set; }
    public int Width { get; private set; }

    public Garden(int lenght, int width)
    {
        Flowers = new List<Flower>();
        Length = lenght;
        Width = width;
    }



    public void PlantFlower()
    {
        foreach (var flower in Flowers)
        {
            if (PlayerX != flower.FlowerX && PlayerY != flower.FlowerY)
            {
                Flowers.Add(new Flower(PlayerX, PlayerY));
            }

            if (PlayerX == flower.FlowerX && PlayerY == flower.FlowerY && flower.CurrentState != "X")
            {
                Flower flowerObject = new Flower(PlayerX, PlayerY, "-");
                Flowers.Add(new Flower
                {
                    CurrentState = "-",
                    FlowerX = PlayerX,
                    FlowerY = PlayerY,
                });

                int i = 0;
                do
                {
                    flowerObject.FlowerTask = Task.Run(() =>
                    {
                        Thread.Sleep(1000);
                        flowerObject.FlowerCt.ThrowIfCancellationRequested();
                        try
                        {
                            flower.CurrentState = flowerObject.States[i];
                            i++;
                            //flower.SCounter++;
                        }
                        catch (OperationCanceledException)
                        {
                            return;
                        }
                    });
                } while (i < flower.States.Length || flower.CurrentState == "X");
            }
        }
    }
}
公共类花
{
公共整数x{get;set;}
公共int-FlowerY{get;set;}
公共字符串[]表示{get;set;}
公共字符串CurrentState{get;set;}
公共任务任务{get;set;}
公共取消标记{get;set;}
公众花卉()
{
}
公共花卉(int-flowerX、int-flowerY、string-currentState)
{
FlowerX=FlowerX;
多花的=多花的;
CurrentState=CurrentState;
}
公共花卉(int flowerX,int flowerY)
{
FlowerX=FlowerX;
多花的=多花的;
States=新字符串[]{-”,“+”,“w”,“w”,“o”,“o”,“X”};
}
}
公营花园
{
public int PlayerX{get;set;}
public int PlayerY{get;set;}
公开列表{get;set;}
公共整数长度{get;私有集;}
公共整数宽度{get;私有集;}
公共花园(内部长、内部宽)
{
鲜花=新列表();
长度=长度;
宽度=宽度;
}
公共空间植物花()
{
foreach(花中的花)
{
如果(PlayerX!=flower.FlowerX&&PlayerY!=flower.FlowerY)
{
添加(新花(PlayerX,PlayerY));
}
如果(PlayerX==flower.FlowerX&&PlayerY==flower.FlowerY&&flower.CurrentState!=“X”)
{
Flower flowerObject=新花(PlayerX,PlayerY,“-”;
花。添加(新的花)
{
CurrentState=“-”,
FlowerX=PlayerX,
花哨的,
});
int i=0;
做
{
flowerObject.FlowerTask=Task.Run(()=>
{
睡眠(1000);
flowerObject.FlowerCt.ThrowIfCancellationRequested();
尝试
{
flower.CurrentState=flowerObject.States[i];
i++;
//flower.SCounter++;
}
捕获(操作取消异常)
{
返回;
}
});
}而(i
问题:为什么需要在此处开始任务?这看起来并不是天生以任务为导向的,但是:我认为这里的问题很大一部分是关于
I
的冲突-您正在以非常不可预测的方式捕获局部变量;您可以尝试执行
var x=i++执行
循环的顶部(在
任务之前运行
),使用
.States[x]
而不是
.States[i]
-并从任务代码中删除
i++
。这是考试项目的一部分。。。我需要遵循详细的说明。所以我必须从这里开始;你有没有试过我提到的关于被俘虏的当地人的修复方法?还有:你认为这里会发生什么?仅仅因为您更新了
CurrentState
,通常不会自动导致任何类型的UI更新或类似情况-如果代码正常工作,这里的成功会是什么样子?特别要注意的是,您可能会在这里创建大量的任务,而
while
循环可能会在第一次睡眠之前运行大量次,因此。。。什么时候应该停止循环?问题:为什么需要在这里开始任务?这看起来并不是天生以任务为导向的,但是:我认为这里的问题很大一部分是关于
I
的冲突-您正在以非常不可预测的方式捕获局部变量;您可以尝试执行
var x=i++执行
循环的顶部(在
任务之前运行
),使用
.States[x]
而不是
.States[i]
-并从任务代码中删除
i++
。这是考试项目的一部分。。。我需要遵循详细的说明。所以我必须从这里开始;你有没有试过我提到的关于被俘虏的当地人的修复方法?还有:你认为这里会发生什么?仅仅因为您更新了
CurrentState
,通常不会自动导致任何类型的UI更新或类似情况-如果代码正常工作,这里的成功会是什么样子?特别要注意的是,您可能会在这里创建大量的任务,而
while
循环可能会在第一次睡眠之前运行大量次,因此。。。什么时候应该停止循环?