Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:54:43+00:00 2026-06-09T07:54:43+00:00

My generalized question is this: how do you write asynchronous code that is still

  • 0

My generalized question is this: how do you write asynchronous code that is still clear and easy to follow, like a synchronous solution would be?

My experience is that if you need to make some synchronous code asynchronous, using something like BackgroundWorker, you no longer have a series of easy to follow program statements that express your overall intent and order of activities, you end up instead with a bunch of “Done” Event Handlers, each of which starts the next BackgroundWorker, producing code that’s really hard to follow.

I know that’s not very clear; something more concrete:

Let’s say a function in my WinForms application needs to start up some amazon EC2 instances, wait for them to become running, and then wait for them to all accept an SSH connection. A synchronous solution in pseudo code might look like this:

instances StartNewInstances() {
    instances = StartInstances()
    WaitForInstancesToBecomeRunning(instances)
    WaitForInstancesToAcceptSSHConnection(instances).
    return (instances)
    }

That’s nice. What is happening is very clear, and the order of program actions is very clear. No white noise to distract you from understanding the code and the flow. I’d really like to end up with code that looks like that.

But in reality, I can’t have a synchronous solution .. each of those functions can run for a long time, and each needs to do things like: update the ui, monitor for time-outs being exceeded, and retry operations periodically until success or time-out. In short, each of these needs to be happening in the background so the foreground UI thread can continue on.

But if I use solutions like BackgroundWorker, it seems like I don’t end up with nice easy to follow program logic like the above. Instead I might start a background worker from my UI thread to perform the first function, and then my ui thread goes back to the UI while the worker thread runs. When it finishes, its “done” event handler might start the next Background Worker. WHen it finishes, its “done” event handler might start the last BackgroundWorker, and so on. Meaning you have to “follow the trail” of the Done Event handlers in order to understand the overall program flow.

There has to be a better way that a) lets my UI thread be responsive, b) let’s my async operations be able to update the ui and most importantly c) be able to express my program as series of consecutive steps (as I’ve shown above) so that someone can understand the resultant code

Any and all input would be greatly appreciated!
Michael

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:54:45+00:00Added an answer on June 9, 2026 at 7:54 am

    My generalized question is this: how do you write asynchronous code that is still clear and easy to follow, like a synchronous solution would be?

    You wait for C# 5. It won’t be long now. async/await rocks. You’ve really described the feature in the above sentence… See the Visual Studio async homepage for tutorials, the language spec, downloads etc.

    At the moment, there really isn’t a terribly clean way – which is why the feature was required in the first place. Asynchronous code very naturally becomes a mess, especially when you consider error handling etc.

    Your code would be expressed as:

    async Task<List<Instance>> StartNewInstances() {
        List<Instance> instances = await StartInstancesAsync();
        await instances.ForEachAsync(x => await instance.WaitUntilRunningAsync());
        await instances.ForEachAsync(x => await instance.WaitToAcceptSSHConnectionAsync());
        return instances;
    }
    

    That’s assuming a little bit of extra work, such as an extension method on IEnumerable<T> with the form

    public static Task ForEachAsync<T>(this IEnumerable<T> source,
                                       Func<T, Task> taskStarter)
    {
        // Stuff. It's not terribly tricky :(
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a generalized version of a previous question regarding Sphinx . Is there
Inspired by this question , the asker assumes that the users of a system
This is a follow-up and complication to this question: Extracting contents of a string
This may be a very generalized question but had anyone has much luck with
I apologize for asking such a generalized question, but it's something that can prove
I am using the jQuery-File-Upload widget (although I believe this question can generalize to
I'm wondering whether an object asking a question to another object that indirectly holds
I find myself having a lot of this in different methods in my code:
This is a silly question, but I can't seem to wrap my head around
I have a string like this: Heading Some interesting text here HeadingSome interesting text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.