C#C
C#4y ago
22 replies
FacePalmGamer

Getting the result from a list of tasks

Its been a minute since I have done async methods but i know i can do a list of tasks I just think I'm using them wrong.
const double LAT_MIN = 37.70649; //y
const double LAT_MAX = 37.77436;
const double LON_MAX = -85.95120;
const double LON_MIN = -85.86829;

List<Task> tasks = new List<Task>();
List<Point> points = new List<Point>();

for (double i = LAT_MIN; i < LAT_MIN; i += .00005)
{
    tasks.Add(CreatePointsAsync(i));
}
foreach (Task task in tasks)
    task.Start();

Task.WhenAll(tasks).Wait();
foreach(Task<List<Point>> task in tasks)
{
    points.InsertRange(points.Count, task.Result);
}

foreach(Point point in points)
{
    Console.WriteLine(point.X + " " + point.Y);
}

async Task<List<Point>> CreatePointsAsync(double y)
{
    List<Point> value = new List<Point>();
    for (double i = LON_MIN; i < LON_MAX; i += .00005)
    {
        value.Add(new Point(i, y));
    }
    return value;
}

public struct Point
{
    public double X;
    public double Y;

    public Point(double X, double Y)
    {
        this.X = X;
        this.Y = Y;
    }
};

I need to create a lot of points so i wanted to split it up to make it faster
Was this page helpful?