C
C#2y ago
Mastalt

✅ Multi-Threading not working

My multithreading isn't working Code:
tasks.Add(
Task.Run(() =>
{
DateTimeOffset findClosestToo = new DateTimeOffset(item.date.Year, item.date.Month, item.date.Day, item.date.Hour, 0, 0, TimeSpan.Zero);
NasaPower result;
Task<NasaPower> request = null;

if ((findClosestToo - previous).Duration() > (findClosestToo - item.date).Duration())
{
request = _client.GetFromJsonAsync<NasaPower>("?start=" + start.ToString("yyyyMMdd") + "&end=" + end.AddDays(1).ToString("yyyyMMdd") + "&latitude=" + item.latitude + "&longitude=" + item.longitude + "&community=re&parameters=ALLSKY_SFC_SW_DWN&format=json&header=true&time-standard=utc");
}
else
{
request = _client.GetFromJsonAsync<NasaPower>("?start=" + start.ToString("yyyyMMdd") + "&end=" + end.AddDays(1).ToString("yyyyMMdd") + "&latitude=" + previousItem.latitude + "&longitude=" + previousItem.longitude + "&community=re&parameters=ALLSKY_SFC_SW_DWN&format=json&header=true&time-standard=utc");
}

request.Wait();
result = request.Result;

solarPotential += result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")];
Console.WriteLine(findClosestToo + "\t\t" + result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")] + "\t\t" + solarPotential);
lock (outputResults)
{
outputResults.Add(new object[] { findClosestToo, result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")], solarPotential });
}
}));
tasks.Add(
Task.Run(() =>
{
DateTimeOffset findClosestToo = new DateTimeOffset(item.date.Year, item.date.Month, item.date.Day, item.date.Hour, 0, 0, TimeSpan.Zero);
NasaPower result;
Task<NasaPower> request = null;

if ((findClosestToo - previous).Duration() > (findClosestToo - item.date).Duration())
{
request = _client.GetFromJsonAsync<NasaPower>("?start=" + start.ToString("yyyyMMdd") + "&end=" + end.AddDays(1).ToString("yyyyMMdd") + "&latitude=" + item.latitude + "&longitude=" + item.longitude + "&community=re&parameters=ALLSKY_SFC_SW_DWN&format=json&header=true&time-standard=utc");
}
else
{
request = _client.GetFromJsonAsync<NasaPower>("?start=" + start.ToString("yyyyMMdd") + "&end=" + end.AddDays(1).ToString("yyyyMMdd") + "&latitude=" + previousItem.latitude + "&longitude=" + previousItem.longitude + "&community=re&parameters=ALLSKY_SFC_SW_DWN&format=json&header=true&time-standard=utc");
}

request.Wait();
result = request.Result;

solarPotential += result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")];
Console.WriteLine(findClosestToo + "\t\t" + result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")] + "\t\t" + solarPotential);
lock (outputResults)
{
outputResults.Add(new object[] { findClosestToo, result.properties.parameter.ALLSKY_SFC_SW_DWN[findClosestToo.ToString("yyyyMMddHH")], solarPotential });
}
}));
13 Replies
phaseshift
phaseshift2y ago
what classifies as 'isnt working'?
Mastalt
Mastalt2y ago
it basically just jumps it, my list outputResults isn't getting filled
phaseshift
phaseshift2y ago
'jumps it' sounds like its throwing an exception
Mastalt
Mastalt2y ago
nope, just runs straight through
Kouhai
Kouhai2y ago
When you place a breakpoint on this line lock (outputResults) does the debugger break or it's never reached?
phaseshift
phaseshift2y ago
wdym 'runs through'? It can't run code and do nothing at the same time
Mastalt
Mastalt2y ago
not run but it just doesn't execute it, it continues without doing anything inside the task, not filling my list and not doing the writeline
phaseshift
phaseshift2y ago
did you debug and watch what happens? Again, it sounds like its throwing
Mastalt
Mastalt2y ago
yep, just skipping over the task, not throwing any exception
phaseshift
phaseshift2y ago
No, if the task throws, you will only know when you get the result or await it Since you're not showing what you do with the task... we don't know if it threw or not it will not be 'just skipping it' ^^
Mastalt
Mastalt2y ago
oh wait im stupid forgot the Task.WaitAll at the end...
mtreit
mtreit2y ago
sync over async <a:Shaking_Eyes:907681222619185233>
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Closed!