If have this code:
Task[] tasks = new Task[3];
int index = 0;
foreach (KeyValuePair<string, int> packageId in PackageIds)
{
await Task.Run(() => _logger.Log(packageId.Key));
tasks[index] = Task.Factory.StartNew(() =>
{
Log(packageId.Key);
});
index++;
}
Task.WaitAll(tasks);
_logger.Log("all done");
This is the Log function:
private void Log(string message)
{
ListViewItem lv = new ListViewItem
{
Text = $"{DateTime.Now:dd-MM-yyyy hh:mm:ss}"
};
lv.SubItems.Add(message);
if (listView1.InvokeRequired)
Invoke(new Action(() => Log(message)));
else
listView1.Items.Add(lv);
}
When I run it I can see the code tries to invoke the listView1. But after that, the application hangs and doesn't continue at all. When I remove the Log() and run it again, it works perfectly.
How can I run this code with the logging?