Demo that shows the difference in performance between the synchronous and asynchronous creation of two large lists. Results will vary but in this run the asynchronous way reduced execution time by 35%. [100-((11/17)*100)]
static void Main(string[] args)
{
initAppConfigData();
AcStopWatch swAsync = new AcStopWatch(logAsync);
using (new AcStopWatchMarker(swAsync))
{
Task<bool[]> arr = fooAsync();
if (arr != null && arr.Result.All(n => n == true))
Console.WriteLine("Lists initialized successfully.");
else
Console.WriteLine("One or more lists failed to initialize.");
}
AcStopWatch swSync = new AcStopWatch(logSync);
using (new AcStopWatchMarker(swSync))
{
if (fooSync())
Console.WriteLine("Lists initialized successfully.");
else
Console.WriteLine("One or more lists failed to initialize.");
}
}
private static void logSync(string elapsedTime)
{
string msg = String.Format("{0} to complete synchronously.", elapsedTime);
Console.WriteLine(msg);
}
private static void logAsync(string elapsedTime)
{
string msg = String.Format("{0} to complete asynchronously.", elapsedTime);
Console.WriteLine(msg);
}
private static async Task<bool[]> fooAsync()
{
AcUsers users = new AcUsers(_dc, _pc);
Task<bool> uini = users.initAsync();
AcDepots depots = new AcDepots();
Task<bool> dini = depots.initAsync();
return await Task.WhenAll(uini, dini);
}
private static bool fooSync()
{
AcUsers users = new AcUsers(_dc, _pc);
Task<bool> uini = users.initAsync();
if (!uini.Result) return false;
AcDepots depots = new AcDepots();
Task<bool> dini = depots.initAsync();
if (!dini.Result) return false;
return true;
}