Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all, am working in winform project . where i have label in FOR LOOP .I want to show the label each time after executing label.text statement . But it doesn't show for every time rather it shows after for loop is finished .

I tried to achieve this by using Thread sleep . But i can't. Please help me .Thank You

Here's My Coding .

C#
for (int i = 1; i <= sourceTable.Rows.Count - 1; i++)
             {

            
                 string checkout;
                  checkout= sourceTable.Rows[i].Field<string>(0);

                  dest = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString);
                  dest.Open();
               destcmd = new SqlCommand(checkout, dest);
               destcmd.ExecuteNonQuery();
               dest.Close();


               prcmail();
               prcmessagecheck();




            
      lblProgress.Text = "Hello "+i;

           

             Thread.Sleep(10000);







                }
Posted
Updated 16-Dec-14 19:53pm
v2
Comments
Kornfeld Eliyahu Peter 17-Dec-14 1:43am    
Are you trying to do some sort of progress bar? Read here: http://www.codeproject.com/Articles/449594/Progress-Bars-Threads-Windows-Forms-and-You
Bns Vigneshwaran 17-Dec-14 1:49am    
leave the progress bar.. i want to show the label as "hello world" for every time it enters into the loop .. Did u Understand my question ?
Sinisa Hajnal 17-Dec-14 2:28am    
The loop executes too fast for you to see anything but the last iteration. You could call lblProgress.Update or .Refresh or .Invalidate and see if redrawing the control helps.
Maciej Los 17-Dec-14 2:17am    
Sorry, but your code is very dangerous for every machine where you'd like to execute it. Move the part where the connection is opened outside the loop!
Secondly, you need to invoke control (label) to be able to see how it change its text.

Assuming your 'for loop is not running in a separate thread:

1. as Maciej recommended, you need to handle opening the connection outside the loop

Consider:

C#
// open the connection

// set an initial message in the TextBox
lblProgress.Text = "Connected. Completing: ";

// in the loop: update
lblProgress.Text += i.ToString() + ", ";

// after the loop completes: clean-up
lblProgress.Text = lblProgress.Text.Trim().TrimEnd(',');
This way there's no need to slow your processing down with using 'Sleep.

However, why not consider using the MS ProgressBar Control ?
 
Share this answer
 
v2
Comments
Maciej Los 17-Dec-14 3:07am    
Thank you, Bill for your comment to my (deleted) answer.
I think that above code does change nothing. Label will be still "frozen". Have you checked it?
BillWoodruff 17-Dec-14 3:26am    
Hi Maciej, yes, I checked this; however, the assumption I'm making here ... which could be wrong ... is that the processing done in the loop will happen quickly, and that showing the cumulative result is a better choice than delaying the processing in order to create an "animated" progress result.
Maciej Los 17-Dec-14 4:28am    
+5 for effort!
Windows Forms Programs run everything in a single thread - the UI thread. Using the Invalidate/Update code tells the compiler to "pause" execution of the function and forces it to update the UI instead of waiting till the end of the function. So use Invalidate and update just use these two methods after setting label.text value. happy coding. So everything in your code is correct except these two line is missing. Just add and see your desired result. happy coding
C#
for (int i = 1; i <= sourceTable.Rows.Count - 1; i++)
{
    string checkout;
    checkout = sourceTable.Rows[i].Field<string>(0);

    dest = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString);
    dest.Open();
    destcmd = new SqlCommand(checkout, dest);
    destcmd.ExecuteNonQuery();
    dest.Close();
    prcmail();
    prcmessagecheck();
    lblProgress.Text = "Hello " + i;

    lblProgress.Invalidate();  //add
    lblProgress.Update();       // add

    Thread.Sleep(10000);
}
 
Share this answer
 
v4
Have you tried to use refresh() method after updating text... like this...

C#
lblProgress.Text = "Hello" +i;
Refresh();
 
Share this answer
 
You should use Thread in this case, and call controls BeginInvoke function to refresh the UI. eg.
C#
Thread backgroundThread = new Thread(
    new ThreadStart(() =>;
    {
        for (int i = 1; i <= sourceTable.Rows.Count - 1; i++)
        {
            //---- Your logic here ---

            Thread.Sleep(50);
            Label.BeginInvoke(
                new Action(() =>;
                    {
                        Label.Text = "your text";
                    }
            ));
       }
    ));
    backgroundThread.Start();

Hope this helps :)
 
Share this answer
 
v2
As you've been told, the right way of doing this is handling the label in another thread. But a simple solution that may work is instead using
C#
Thread.Sleep
, using
C#
Application.DoEvents()


Give it a try.
 
Share this answer
 
Comments
Maciej Los 17-Dec-14 3:07am    
Have you tried to use your solution with c#?
Pikoh 17-Dec-14 3:38am    
In fact i had not tried it. As i told,i always use background workers for this kind of task. But after your comment,i tried it and it works.I have a small sample if you wannt to see it.
Maciej Los 17-Dec-14 4:23am    
No, thank you, i'm not questioner ;)
Pikoh 17-Dec-14 4:37am    
I know..just trying to satisfy your curiosity :)

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