I would recommend not to block any threads and to update the ProgressBar
only on the UI thread. To update the ProgressBar
, calculate the percentage of the total work that each file represents and update the bar by that value after each job is completed. Here's an example method:
private async Task<DataSet> DataSetBuilder(IEnumerable<string> stringCollection, ProgressBar progressBar)
{
int percentagePerFile = 100 / stringCollection.Count();
DataSet dts = new DataSet();
foreach (string file in stringCollection)
{
DataTable tbl = await Task.Run<DataTable>(() =>
{
return CSVtoDataTable(file);
});
progressBar.Value += percentagePerFile;
dts.Tables.Add(tbl);
}
return dts;
}