Click here to Skip to main content
15,749,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using web forms
I made a simple WF in C# in an attempt to change the label dynamically.

However, when I run this code, there is no visible change, until after the code has run, and then it changes to "Processing 9" 0-8 are not ever shown. Is it because it is within the loop?

What I have tried:

private void button1_Click(object sender, EventArgs e)
       for (int i = 0; i < 10; i++)
           label9.Text = "Processing " + i.ToString();
Updated 2-Nov-16 18:47pm

The problem is that you are running that code on the UI thread - because if you weren't you would get an "cross threading error" - which means that the code that is responsible for updating your display cannot run until that method exits. Because it doesn't exit but goes to sleep instead, it isn't running to update the display, so you never see the number change.

There are ways to get what you want - i.e. the label changing every second - but exactly how you do it depends on the environment you are working in: a WinForms solution would be different to a website based solution for example. And there are different ways to do it, depending on how complicated you can cope with, and how much bad code you can tolerate.
I'm going to assume a WinForms solution, and since you are a beginner, I'm going to ignore the "bad code" option as well, for reasons it would take far too long to explain.
That leave you two options:
1) Multi-threading.
2) Timer.

The threading option gets very complicated, because you need to create a new thread, and then pass some code back to the original thread to actually change the label (or you get the cross threading error I mentioned earlier) so I'm going to put that on the shelf as well, and explain the simplest "proper" way to do it.

Delete your loop.
Create a class level integer, called count.
private int count = 0;

Add a Class level Timer instance, called updateLabel
private System.Windows.Forms.Timer updateLabel = new System.Windows.Forms.Timer();

In your Click event, add this code:
updateLabel.Interval = 1000;
updateLabel.Tick += updateLabel_Tick;

Now add a handler for the Tick event:
void updateLabel_Tick(object sender, EventArgs e)
    label1.Text = "Processing " + (++count).ToString();

Run your code.
Share this answer
try using Window setInterval() Method[^]

<html xmlns="">
<head runat="server">
        function doProcess() {
            var i = 0;
            var interval = window.setInterval(function () {
                document.getElementById('<%=lbl.ClientID%>').textContent = "Processing " +  i;
               if (i >= 10)
           }, 1000);

    <form id="form1" runat="server">
        <asp:Label Text="" ID="lbl" runat="server" />
        <asp:Button Text="Submit" OnClientClick="doProcess();return false;" runat="server" />
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900