Tasks
Tasks are very useful to run background processes and allowing the UI to
run normally. You can also run multiple tasks at the same time and the CPU
will run then at the same time as long as the resources are available.
Code for running the tasks are in the lambda format.
i.e. Task.Run(() => someCode());
For User Interfaces, you may wish to run processing during the startup and will
need to know when the process has finished. For this you will need to use the 'await'
keyword. For this, the event will need the 'async' key word as well.
See the code below as an example.
private async void RunUpdates() { if (strDataOutputFilename != "") { WriteOutputData("Updated @ " + DateTime.Now.ToString(), false); } strAlerts.Clear(); strUpdates.Clear(); strCounts.Clear(); await Task.Run(() => GetAlertsData()); await Task.Run(() => GetUpdatesData()); await Task.Run(() => GetCountsData()); AddAlertsToDataGrid(); AddUpdatesToDataGrid(); AddCountsToDataGrid(); Application.DoEvents(); }
For multiple tasks, you can test for all finishing by using the 'When.All' keyword.
See the code below as an example.