2

In my c# winform app I have a button which when clicked, it performs some calculations which can tke some time.

I also have a label.visible = false which I would like to label.visible = true right after the button click so that the user can see the app working away.

The thing is even when label.visible = true is the first thing in the button1_Click(object sender, EventArgs e) method, it only toggles to visible at the very end, after the calculation is performed.

How can I show the label right after the button_click?

sd_dracula
  • 3,796
  • 28
  • 87
  • 158

4 Answers4

4

If you need your application to remain responsive whilst your app is processing, take a look at Background Worker

You can force your form to update & process any background messages by calling:

Application.DoEvents();

Just after you have changed your label - although this is probably a bit of a hacky solution.

Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • You really should put `BackgroundWorker` on top and also discourage `DoEvents`. Take a look at http://stackoverflow.com/questions/5181777/use-of-application-doevents – Alvin Wong Jan 07 '13 at 14:26
2

Your calculation is being performed on the UI thread. This means that it is blocking the UI from refresshing after you have set the visibility of the label. You should consider doing the calculation on another thread using a Task. This will allow the UI to be responsive during what it seems is a long operation that can run in the background.

e.g.

var taskCalc = Task.Factory.StartNew(() => //Do Calculation );
Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
1

There are several ways to tackle this.

One way (the simplest) is to call

this.Refresh();

right after you set the Label to visible.

Another is to do your calculations in a background thread. The easiest way to do that is to use a BackgroundWorker. Then your main thread can just continue serving the UI (refreshing the form, responding to buttons etc.) while the background worker thread performs the computation.

See http://www.dotnetperls.com/backgroundworker for more on background worker threads.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 1
    Yes the refresh works fine but I will go ahead and change the code to backgroundworker as it is neater and it will not hang the UI. Thank you – sd_dracula Jan 07 '13 at 14:31
1

Like others have said: You are performing your work on the UI Thread. I am showing you another valid way of achieving what you need. You can move your work to a separate thread using an anonymous delegate.

(new System.Threading.Thread(() =>
        {
            dowork(); // What ever work you need put here.
        })).Start();
Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • The code above, where can it go? or it doesn't matter, it can be even in the button_click method as it is run by another thread? – sd_dracula Jan 07 '13 at 15:47
  • You can run it in the button_Click Method. Or create a new method and call it from the button_Click method. – Botonomous Jan 07 '13 at 15:49