Hello I have the following code that creates a task. Then sets it to start. The task is meant to add a response to the ConcurrentBag list. But the await does not seem to be waiting for all the tasks to complete. But they are being marked as completed. There is only one task in the list. It works fine when using Task.Run!
public async void RunT1() { DoesNotWork(); } public async void RunT2() { DoesWork(); } public async void DoesNotWork() { ConcurrentBag<string> concurrentBag = new ConcurrentBag<string>(); List<Task> taskList = new List<Task>(); Task task1 = new Task(async () => { var xml = await LongWorkingMethod(); concurrentBag.Add(xml); }); taskList.Add(task1); taskList[0].Start(); await Task.WhenAll(taskList); if (concurrentBag.Count > 0) //concurrentBag is empty, //even though the task has finished { Debug.Print("success"); } } public async void DoesWork() { ConcurrentBag<string> concurrentBag = new ConcurrentBag<string>(); List<Task> taskList = new List<Task>(); Task task1 = Task.Run(async () => { var xml = await LongWorkingMethod(); concurrentBag.Add(xml); }); taskList.Add(task1); await Task.WhenAll(taskList); if (concurrentBag.Count > 0) //concurrentBag is NOT empty { Debug.Print("success"); } }