Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
What is a good alternative to await Task.Run executing an asyncronous lambda?

I'm in the process of upgrading old code using BackgroundWorker. Following a series of articles from Stephen Cleary on this matter, I think the ideal substitute in this case is Task.Run().

Mine is a Windows Forms application. When a Form is constructed, different background operations are started and their results are collected in different event handlers, as per the BackgroundWorker pattern.

Now, my new methods follow the async/await pattern. I have some concerns about calling Task.Run() on an async methods.

private async void FormEventHandler(object sender, EventArgs e)
{
    Task.Run(async () => await AwaitableLongRunningTask_1());
    Task.Run(async () => await AwaitableLongRunningTask_2());
    //...
}

AwaitableLongRunningTask_1 and _2 needs to be async, since the calls they makes within the method body are to async methods.

I can't call them in a simple await succession:

private async void FormEventHandler(object sender, EventArgs e)
{
    await AwaitableLongRunningTask_1();
    await AwaitableLongRunningTask_2();
    //...
}

since they need to be started in parallel and they're obviously long running tasks spanning several seconds.

So my question is, do I need to refactor AwaitableLongRunningTasks to be non-async void, changing their internal behaviour like suggested here in "How to call asynchronous method from synchronous method"? Are there better options I'm missing?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use Task.WhenAll

private async void FormEventHandler(object sender, EventArgs e)
{
    await Task.WhenAll( 
            AwaitableLongRunningTask_1(), 
            AwaitableLongRunningTask_2());

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!