bageesh
bageesh

Reputation: 83

Code inside for loop working only after the loop is completed

I have a function called ExecuteLoop as

private void ExecuteLoop()
{ 
 for(int i=0;i<10000;i++)
 lable1.Text=i;
}

but the label text is not changing..At the end of the loop its showing like "10000"

What to do?

Upvotes: 0

Views: 60

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415780

I assume you're using winforms? The winforms controls hide a lot of detail about what's really going on. At a lower level, your program will rely on code in the .Net framework that looks something like this:

while (true)
{
    var message = GetMessagesFromOperatingSystem();
    HandleReceivedMessage(message);
    if (message == WM_QUIT) return;
}

This is called the message pump or message loop. That's not the real code, but it should give you the idea.

When you change a label text, or set any other property on a control, the setter for that property calls a function which tells the operating system that the screen space allocated for your control is now invalid, and needs to be re-drawn. Windows will then post a message back to your program that it needs to redraw that space. This message is received by the code demonstrated above, and that code is in the same thread as your for loop. The net result of all this is that no changes to your control properties will be shown on the screen until the method ends.

There are a few ways around this. The best one for your situation will depend on what you're really trying to do. As quick examples, if you're trying to show a clock you'll likely want to use a timer component, and move your code to the timer's tick event, rather than use a delay in a loop. If this is a placeholder for more complicated work that will be added in later, you probably want to use a BackgroundWorker component (though it will see overkill at first for something so simple).

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

it does not show the intermediate values as you are not adding delay in between use Timer to see the Label Text changing from 0 to 10000.

Try this:

public partial class Form1 : Form
    {
       int count = 0;
       int maxlimit=10000;
        System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            SetTimer(500);
        }
        private void SetTimer(int milliseconds)
        {            
            timer1.Interval = milliseconds;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
        }
        private void timer1_Tick(Object o, EventArgs e)
        {
            if (count < maxlimit)
            {
                label1.Text = count.ToString();
                count++;
            }
            else
            {
                count = 0;
                button1.Enabled = true;
                label1.Text = "completed!";
                timer1.Stop();

            }
        }

Upvotes: 0

Related Questions