0

我想创建一个任务数组,称为 listTask,listTask 的每个元素都是 A 类型的任务,A 类型的任务由函数 Task.WhenAll 创建。然后我等待 Task.WhenAll(listTask) 但程序不执行 listTask 数组中的工作。我设置了调试,那些任务都完成了,我不明白为什么

namespace Ding.LearningNewThings
{
    public class MultiChannelTask
    {
        public static async Task RunMiltipleChannel()
        {
            ConcurrentDictionary<int, Channel<Position>> _dataChannels = new ConcurrentDictionary<int, Channel<Position>>();

            var listPlace = Place.InitData();

            var numberOfPlace = listPlace.Count();

            for (int i = 0; i < listPlace.Count(); i++)
            {
                _dataChannels.TryAdd(i, Channel.CreateUnbounded<Position>());
            }

            Task[] listStationTask = new Task[numberOfPlace];

            for (var j = 0; j < numberOfPlace; j++)
            {
                var listTask = new Task[2];
                var placeOuter = listPlace[j];

                listTask[0] = Task.Run(async () =>
                {
                    int IndexOfPlace = j;
                    var place = new Place()
                    {
                        ID = placeOuter.ID,
                        Name = placeOuter.Name
                    };

                    Channel<Position> dataChannel;
                    var r = new Random();

                    if (_dataChannels.TryGetValue(IndexOfPlace, out dataChannel))
                    {
                        var position = new Position()
                        {
                            PlaceID = place.ID,
                            PlaceName = place.Name,
                            ID = r.Next(1, 100)
                        };

                        await dataChannel.Writer.WriteAsync(position);
                        Console.WriteLine($"Push postion ID {position.ID}, Place ID {position.PlaceID}");
                    }
                });

                listTask[1] = Task.Run(async () =>
                {
                    var IndexOfPlace = j;

                    Channel<Position> dataChannel;
                    var r = new Random();

                    if (_dataChannels.TryGetValue(IndexOfPlace, out dataChannel)) {

                        var position = await dataChannel.Reader.ReadAsync();

                        Console.WriteLine($"Get postion ID {position.ID}, Place ID {position.PlaceID}");
                    }
                });

                listStationTask[j] = Task.WhenAll(listTask);
            }

            await Task.WhenAll(listStationTask);
        }
    }

    public class Place
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public static List<Place> InitData()
        {
            var listData = new List<Place>();

            for (int i = 0; i < 10; i++)
            {
                var data = new Place()
                {
                    ID = i,
                    Name = $"Postion{i}",
                };

                listData.Add(data);
            }
            return listData;
        }
    }

    public class Position
    {
        public int ID { get; set; }
        public int PlaceID { get; set; }
        public string PlaceName { get; set; }
        public string Name { get; set; }

        public static List<Position> InitData()
        {
            var listData = new List<Position>();

            for (int i = 0; i < 10; i++)
            {
                var data = new Position()
                {
                    ID = i,
                    Name = $"Postion{i}"
                };

                listData.Add(data);
            }
            return listData;
        }
    }
}

我似乎已经提前完成了任务。有时它可以工作,但我不知道为什么作业总是在没有在列表任务代码中运行的情况下完成。

4

0 回答 0