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 8744839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:54:26+00:00 2026-06-13T11:54:26+00:00

I have been reading up on the async/await syntax introduced in .Net 4.5 and

  • 0

I have been reading up on the async/await syntax introduced in .Net 4.5 and getting a feel of it.

I have found simple samples where Task.Yield is the way to go rather than Application.DoEvents. I tried one of the samples (filling in the blanks)

Code on Form1 with 1 button:

public async void button1_Click(object sender, EventArgs e) {
    var list = new List<int>();
    for (int i = 0; i < 10000; i++) {
        list.Add(i);
    }
    for (int i = 0; i < list.Count; i++) {
        Process(list[i]);
        await Task.Yield();
        //await Task.Delay(1);
    }
}

public static void Process(int i) {
    Debug.WriteLine(i);
}

However, running this code, the UI thread is blocked or I believe it is blocked as I can not move the window while the code runs. If I comment out the Task.Yield() and use the Task.Delay(1) line instead, the GUI is responsive.

So have I misunderstood something here? I know using DoEvents and the like is bad practice, but I have some legacy code I am responsible for that uses this, and I aim to replace it with Yield as it is the best choice. But first I need to get warm with async/await.

  • 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-13T11:54:27+00:00Added an answer on June 13, 2026 at 11:54 am

    async doesn’t magically make your code good. Yield is not a direct replacement for DoEvents (which is evil). It will take some work to change the alignment of your code.

    As long as you don’t need the UI context, you can just push your work off to the threadpool:

    public async void button1_Click(object sender, EventArgs e) {
      await Task.Run(() => {
        var list = new List<int>();
        for (int i = 0; i < 10000; i++) {
          list.Add(i);
        }
        for (int i = 0; i < list.Count; i++) {
          Process(list[i]);
        }
      });
    }
    

    Alternatively, you can consider breaking up the UI-specific parts either using IProgress<T> or using Task.Run for the non-UI work:

    public async void button1_Click(object sender, EventArgs e) {
      var list = new List<int>();
      await Task.Run(() => {
        for (int i = 0; i < 10000; i++) {
          list.Add(i);
        }
      });
    
      for (int i = 0; i < list.Count; i++) {
        await ProcessAsync(list[i]);
      }
    }
    
    public static async Task ProcessAsync(int i) {
      await Task.Run(() => { ... }); // background
      myUi.Text = i.ToString() + " working"; // ui
      await Task.Run(() => { ... }); // more background
      myUi.Text = i.ToString() + " complete"; // ui
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been reading about async and tasks and been attempting to convert the CopyFileEx
I have been reading about MVP, MVVM and have used asp.net MVC in couple
I have been reading more about C#'s new async keyword and support for it
I have been reading a lot about Reinforcement Learning lately, and I have found
I've been reading about the new async await keyword and it sounds awesome, but
I've been reading about the new async and await operators in C# and tried
I've been reading some async articles here: http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 and the author says : When
I have been reading up on the c++ auto_ptr and unique_ptr and stuff and
I have been reading Rafactoring by Martin Fowler and in the beginning of the
I have been reading up on the changes that .NET4.5 will bring, and on

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.