Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. StatusStrip does not update lable

StatusStrip does not update lable

Scheduled Pinned Locked Moved C#
beta-testingquestionannouncementcode-review
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    s196675m
    wrote on last edited by
    #1

    Hello I have a lengthly process which takes sometime couple of minutes. I like to update the statusbar to give some feedback to the user. When I tried to update the label of a statusstip from inside a lengthly loop, statusstrip does not show the updated result until it finishes the all iteration. Here is my experiemental function. for (int i = 1; i <= 50000; i++) { System.String str = "Iteration # "; // deliberately created inside the loop str += i.ToString(); if ( (i % 1000) == 0) { toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. } } When I run this function, it just display the final result which show "Iteration # 50000". It does not show any intermediate iteration. Is it blocking the thread? Even though I put time consuming operation inside the function, still it does not show the intermediate statusstrip update. Does any body know, how I can update the lable of a statusstrip to update the iteration number within a loop. Thank you in advance.

    C L 2 Replies Last reply
    0
    • S s196675m

      Hello I have a lengthly process which takes sometime couple of minutes. I like to update the statusbar to give some feedback to the user. When I tried to update the label of a statusstip from inside a lengthly loop, statusstrip does not show the updated result until it finishes the all iteration. Here is my experiemental function. for (int i = 1; i <= 50000; i++) { System.String str = "Iteration # "; // deliberately created inside the loop str += i.ToString(); if ( (i % 1000) == 0) { toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. } } When I run this function, it just display the final result which show "Iteration # 50000". It does not show any intermediate iteration. Is it blocking the thread? Even though I put time consuming operation inside the function, still it does not show the intermediate statusstrip update. Does any body know, how I can update the lable of a statusstrip to update the iteration number within a loop. Thank you in advance.

      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #2

      According to your experimental function provided, it should work. Is the real function similar in logic and sequence to this one? Also, I would clear the text then re-set it (This is not a solution to your problem, but recommendation.) Normally operation which take so long can be put into a separate thread and provide feedback to user at same time in a different thread.

      S 1 Reply Last reply
      0
      • C CodingYoshi

        According to your experimental function provided, it should work. Is the real function similar in logic and sequence to this one? Also, I would clear the text then re-set it (This is not a solution to your problem, but recommendation.) Normally operation which take so long can be put into a separate thread and provide feedback to user at same time in a different thread.

        S Offline
        S Offline
        s196675m
        wrote on last edited by
        #3

        Yes, real function is same as this one. statusbar only update once it finishes the loop, does not updata any intermediate iteration. My guess is that, UI thread might be freeze until loop finishes. Otherwise I am not sure why it does not update in between. Still looking for some more help. Thank you.

        C 1 Reply Last reply
        0
        • S s196675m

          Hello I have a lengthly process which takes sometime couple of minutes. I like to update the statusbar to give some feedback to the user. When I tried to update the label of a statusstip from inside a lengthly loop, statusstrip does not show the updated result until it finishes the all iteration. Here is my experiemental function. for (int i = 1; i <= 50000; i++) { System.String str = "Iteration # "; // deliberately created inside the loop str += i.ToString(); if ( (i % 1000) == 0) { toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. } } When I run this function, it just display the final result which show "Iteration # 50000". It does not show any intermediate iteration. Is it blocking the thread? Even though I put time consuming operation inside the function, still it does not show the intermediate statusstrip update. Does any body know, how I can update the lable of a statusstrip to update the iteration number within a loop. Thank you in advance.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if all this code is running on the GUI thread (say inside a button click handler), then it is normal the GUI is dead until your code finishes, at which point it would show the final status text only. You could fix that by adding a line Application.DoEvents() after changing label.Text (this is a dangerous hack, if your code gets re-entered a stack overflow may occur, so disable the button that started this for as long as it takes). if all this code is supposed to run on a separate thread (in an attempt to keep the GUI alive), then you need to use Control.InvokeRequired and Control.Invoke to update any GUI component. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          S 1 Reply Last reply
          0
          • S s196675m

            Yes, real function is same as this one. statusbar only update once it finishes the loop, does not updata any intermediate iteration. My guess is that, UI thread might be freeze until loop finishes. Otherwise I am not sure why it does not update in between. Still looking for some more help. Thank you.

            C Offline
            C Offline
            CodingYoshi
            wrote on last edited by
            #5

            I am not sure where you are initiating the function from. Is it in an event handler? In any case try the following and it should work. The idea is to create a thread and ask the thread to do some work for you--in your case do the loop. Then ever 100 iteration ask the form to update itself using a delegate. You should improve this code as per your need but here it is anyways. public delegate void UpdateLabel(); private Thread loopThread = null; private string str; private void button1_Click(object sender, EventArgs e) { loopThread = new Thread(new ThreadStart(DoTheLoop)); loopThread.Start(); //aThread.Join(); } private void DoTheLoop() { for (int i = 1; i <= 500000; i++) { str = "Iteration # "; // deliberately created inside the loop str += i.ToString(); if ((i % 100) == 0) { //toolStripStatusLabel1.Text = string.Empty; //toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. // Ask the form to Invoke the delegate and ask it to update the label this.Invoke(new UpdateLabel(UpdateStatusStrip)); } // ask the loopThread to sleep for a while so processor can do something else Thread.Sleep(10); } } private void UpdateStatusStrip() { toolStripStatusLabel1.Text = string.Empty; toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. } Let me know if you have any questions.

            S 1 Reply Last reply
            0
            • C CodingYoshi

              I am not sure where you are initiating the function from. Is it in an event handler? In any case try the following and it should work. The idea is to create a thread and ask the thread to do some work for you--in your case do the loop. Then ever 100 iteration ask the form to update itself using a delegate. You should improve this code as per your need but here it is anyways. public delegate void UpdateLabel(); private Thread loopThread = null; private string str; private void button1_Click(object sender, EventArgs e) { loopThread = new Thread(new ThreadStart(DoTheLoop)); loopThread.Start(); //aThread.Join(); } private void DoTheLoop() { for (int i = 1; i <= 500000; i++) { str = "Iteration # "; // deliberately created inside the loop str += i.ToString(); if ((i % 100) == 0) { //toolStripStatusLabel1.Text = string.Empty; //toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. // Ask the form to Invoke the delegate and ask it to update the label this.Invoke(new UpdateLabel(UpdateStatusStrip)); } // ask the loopThread to sleep for a while so processor can do something else Thread.Sleep(10); } } private void UpdateStatusStrip() { toolStripStatusLabel1.Text = string.Empty; toolStripStatusLabel1.Text = str; // Update statusstrip lebel every 1000 iteration. } Let me know if you have any questions.

              S Offline
              S Offline
              s196675m
              wrote on last edited by
              #6

              Hi Thanks for your advice. I tried your idea. It works but it does not show all the updates instead it skips many updates. when I wrote === if ((i % 10 ) == 0) == this condition, it does not show all 10, 20 ,30 ..... etc. Sometimes it jumps from 12450 to 16240 (as an example). It looks like before updating one call, loops continue and many calls are discarded because previous one still not finish and when one updating finish, it does not attemp to update next one instead updating the current one. I tried another idea from another poster from this thread and he suggested to use Application.DoEvents() and it works fine and shows all the updates sequentially. Thank you for your advice.

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, if all this code is running on the GUI thread (say inside a button click handler), then it is normal the GUI is dead until your code finishes, at which point it would show the final status text only. You could fix that by adding a line Application.DoEvents() after changing label.Text (this is a dangerous hack, if your code gets re-entered a stack overflow may occur, so disable the button that started this for as long as it takes). if all this code is supposed to run on a separate thread (in an attempt to keep the GUI alive), then you need to use Control.InvokeRequired and Control.Invoke to update any GUI component. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                S Offline
                S Offline
                s196675m
                wrote on last edited by
                #7

                Thank you for your suggession. I tried your idea and it works in my test case.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups