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;
}
};
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
13 Replies
FacePalmGamer
FacePalmGamerOP3y ago
so dies it return the value of all the tasks i have running? because doing var test = Task.WhenAll(tasks); says test is just a task not a array of any sort
phaseshift
phaseshift3y ago
The value is in the individual task Which, btw, create points async is synchronous
FacePalmGamer
FacePalmGamerOP3y ago
what am i doing wrong, i thought if it was async and you ran it on a task it ran in a different thread
phaseshift
phaseshift3y ago
Common misconception You don't have any awaits in your async method, so it will just be synchronous If you need to run CPU heavy code in a thread, use Task.Run
FacePalmGamer
FacePalmGamerOP3y ago
can i just add an await before the return?
phaseshift
phaseshift3y ago
If you added an await as the first line that would work. Just before the return... Nope
FacePalmGamer
FacePalmGamerOP3y ago
how do i do it your way, with just Task.Run assume that is the way I'm supposed to do something like this
phaseshift
phaseshift3y ago
Change the create points method to just return a list of point, no async or task
FacePalmGamer
FacePalmGamerOP3y ago
tasks.Add(Task.Run(() => CreatePoints(i)));?
phaseshift
phaseshift3y ago
Yes
FacePalmGamer
FacePalmGamerOP3y ago
or am i not adding them to a list anymore either ok cool ok so its not giving me anything. like its not running at all since its finishes instantly
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<List<Point>>> tasks = new List<Task<List<Point>>>();
List<Point> points = new List<Point>();

for (double i = LAT_MIN; i < LAT_MIN; i += .00005)
{
tasks.Add(Task.Run(() => CreatePoints(i)));
}

var results = await Task.WhenAll(tasks);
foreach (List<Point> result in results)
{
points.InsertRange(points.Count, result);
}

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

Console.WriteLine("fin");

List<Point> CreatePoints(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;
}
};
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<List<Point>>> tasks = new List<Task<List<Point>>>();
List<Point> points = new List<Point>();

for (double i = LAT_MIN; i < LAT_MIN; i += .00005)
{
tasks.Add(Task.Run(() => CreatePoints(i)));
}

var results = await Task.WhenAll(tasks);
foreach (List<Point> result in results)
{
points.InsertRange(points.Count, result);
}

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

Console.WriteLine("fin");

List<Point> CreatePoints(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;
}
};
phaseshift
phaseshift3y ago
Look at your first for loop
FacePalmGamer
FacePalmGamerOP3y ago
ah yeah that will do it
Want results from more Discord servers?
Add your server