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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:25:25+00:00 2026-05-16T07:25:25+00:00

private void button1_Click(object sender, RoutedEventArgs e) { int i = 0; while (i <

  • 0
private void button1_Click(object sender, RoutedEventArgs e)
{
        int i = 0;

        while (i < 500)
        {
            label1.Content = i.ToString();
        //  System.Threading.Thread.Sleep(2000);
            ++i;
        }
 }

I am trying to update the content of a Label each time a variable is incremented, but what happens is label1’s Content is changed only once and only after the while loop terminates. I thought that the incrementation of the counter variable was so fast that the UI thread could not catch up with it, so I wanted to make the thread idle for 2 seconds, hoping to see label1 change value 500 times. It did not work either. Why?

  • 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-16T07:25:26+00:00Added an answer on May 16, 2026 at 7:25 am

    As others have said, the problem is that you’re blocking the UI thread.

    Rather than use the suggestions with Application.DoEvents, I would suggest that you use a DispatcherTimer to schedule the update. Sample code:

    DispatcherTimer timer = new DispatcherTimer();
    timer.Tick += delegate
    {
        label.Content = counter.ToString();
        counter++;
        if (counter == 500)
        {
            timer.Stop();
        }
    };
    timer.Interval = TimeSpan.FromMilliseconds(2000);
    timer.Start();
    

    (I’m not sure whether Application.DoEvents even works in WPF, and it’s generally regarded as bad practice anyway. If it does work, I doubt that it’s guaranteed to work in future versions. Mixing the two UIs in the same application just for the sake of this sounds like a really bad idea to me.)

    You could use a Timer (either System.Threading.Timer or System.Timers.Timer) but in both cases you’d then need to marshal back to the dispatcher thread anyway. DispatcherTimer makes this simpler – the Tick event is always fired within the Dispatcher thread anyway.

    EDIT: If you really want an equivalent of DoEvents, here’s code which I’ve verified does work, and is based on the MSDN docs for Dispatcher.PushFrame explaining how to do the equivalent of Application.DoEvents but from WPF:

    public void DoEvents()
    {
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new DispatcherOperationCallback(ExitFrames), frame);
        Dispatcher.PushFrame(frame);
    }
    
    public object ExitFrames(object f)
    {
        ((DispatcherFrame)f).Continue = false;
    
        return null;
    }
    
    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 500; i++)
        {
            label.Content = i.ToString();
            DoEvents();
        }
    }
    

    I still wouldn’t recommend this though – WPF (like Windows Forms) is designed on an event-driven way of working.

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

Sidebar

Related Questions

I have this code: void wait(int ms) { System.Threading.Thread.Sleep(ms); } private void button1_Click(object sender,
private void btnSend_Click(object sender, RoutedEventArgs e) { Button obj=(Button)sender; obj.Content=Cancel; SendImage send = new
private void button1_Click(object sender, EventArgs e) { for (int i = 0; i <
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
I tried to login to gmail with the code: private void button1_Click(object sender, EventArgs
Here's the Problem: private void editTaskButton_Click(object sender, RoutedEventArgs e) { // Cast the parameter
I have this piece of code in my Silverlight project: private void button1_Click(object sender,
Got this line of code here but its not working. private void Button_Click(object sender,
The following code is working great in a normal console application: private void button1_Click(object
I can't figure out why this simple update command won't work: private void button1_Click(object

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.