public partial class MultiTaskThingy : UserControl
{
public MultiTaskThingy()
{
InitializeComponent();
}
public async Task<AsyncStatus> RunMultipleTasks(AsyncProgress<AsyncStatus> progress, CancellationToken cToken)
{
AsyncThingy thingy = new AsyncThingy();
AsyncStatus overallStatus = new AsyncStatus();
var localProgress = new AsyncProgress<AsyncStatus>(ReportProgress);
List<Task<AsyncStatus>> taskList = new List<Task<AsyncStatus>>() { thingy.PseudoTask("1", localProgress, cToken), thingy.PseudoTask("2", localProgress, cToken), thingy.PseudoTask("3", localProgress, cToken) };
try
{
await Task.WhenAll(taskList);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.Message);
}
return overallStatus;
}
void ReportProgress(AsyncStatus value)
{
switch (value.Context)
{
case "1":
progressBar1.Invoke(pb => { pb.Value = value.PercentComplete; });
break;
case "2":
progressBar2.Invoke(pb => { pb.Value = value.PercentComplete; });
break;
case "3":
progressBar3.Invoke(pb => { pb.Value = value.PercentComplete; });
break;
default:
throw new ArgumentOutOfRangeException($"Context {value.Context} is not valid");
}
}
}
I then have that UserControl on a Windows Form with a single button that kicks things off. The button code is: private async void buttonDoManyThings_Click(object sender, EventArgs e)
{
_cts = new CancellationTokenSource();
var progressIndicator = new AsyncProgress<AsyncStatus>(ReportProgress);
await Task.Run(() => multiTaskThingy.RunMultipleTasks(progressIndicator, _cts.Token));
_cts.Dispose();
_cts = null;
}
All 3 tasks run and the calling UI thread isn't blocked. However, the tasks run sequentially rather than concurrently. This isn't what I expected. Can anyone tell me what I'm doing wrong?Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.