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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:55:28+00:00 2026-05-23T09:55:28+00:00

I am trying to make a stop button for this loop but it runs

  • 0

I am trying to make a stop button for this loop but it runs indefinite, nothing happens when i click button 2

bool dowhile = false;
private void button1_Click(object sender, EventArgs e)
{
    do
    {
        for (int i = listbox1.Items.Count - 1; i >= 0; i--)
        {
            string textstring = listbox1.Items[i].ToString();
            richTextBox1.AppendText("" + textstring + ": Done\n");
            Thread.Sleep(1000);
        }
    } while (!dowhile);
}
private void button2_Click(object sender, EventArgs e)
{
    this.dowhile = true;
}

where do i go wrong ?

sry for the “lvlchanger” typo, code is ok now, nothing missing

i’m also looking for a not-so-long fix on this :))

  • 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-05-23T09:55:29+00:00Added an answer on May 23, 2026 at 9:55 am

    Thread.Sleep is almost always the incorrect approach; when you find yourself wanting to use something like it or Application.DoEvents in your code, it’s time to take a long step back and think about what you’re really doing and why it isn’t working. A new design should be in your future.

    The real problem here is that you’re doing work on the UI thread, which blocks the user from interacting with your application. Your click on Button2 is getting recognized and processed after the loop has already finished processing, at which point it does absolutely nothing.

    This is because threads can only complete one task at a time. And, in fact, Thread.Sleep(100) only makes this worse, because it forces the thread to spin and do nothing for 100 milliseconds. That’s 100 more milliseconds it will take for the loop to complete for absolutely no gain.

    The correct solution to this (common) problem is to spin the loop off onto a separate thread, using something like the BackgroundWorker class. The MSDN entry has a very good example of its use, including your specific use case: allowing the user to cancel a long-running background task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make the case for click-once and smart client development but my
I am trying to make a div, that when you click it turns into
I have a GUI C# application that has a single button Start/Stop. Originally this
I am trying to make little photo gallery with help of model view. But
I am trying to make a stop watch with NSTimer. I gave the following
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make a data bound column invisible after data binding, because it

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.