I believe I might just have the syntax wrong but what I'm trying to do is create a task that runs after another task is finished.
I have a task for each array of 100 in a list. It starts a new thread passing that array into a method. The method returns a dictionary when it finishes. I'm trying to create a task to run after the method is finishes where it passes the returned dictionary to a separate method that does some more work.
static void Main(string[] args)
{
try
{
stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
startDownload();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static async void startDownload()
{
try
{
DateTime currentDay = DateTime.Now;
if (Helper.holidays.Contains(currentDay) == false)
{
List<string> markets = new List<string>() { "amex", "global", "nasdaq", "nyse" };
Parallel.ForEach(markets, async market =>
{
try
{
IEnumerable<string> symbolList = Helper.getStockSymbols(market);
var historicalGroups = symbolList.Select((x, i) => new { x, i })
.GroupBy(x => x.i / 100)
.Select(g => g.Select(x => x.x).ToArray());
Task<Dictionary<string, string>>[] historicalTasks =
historicalGroups.Select(x => Task.Run(() =>
Downloads.getHistoricalStockData(x, market)))
.ToArray();
Dictionary<string, string>[] historcalStockResults = await
Task.WhenAll(historicalTasks);
foreach (var dictionary in historcalStockResults)
{
Downloads.updateSymbolsInDB(dictionary);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
});
await Task.Delay(TimeSpan.FromHours(24));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}