Home > Back-end >  How to make multiple labels appear at different times C#
How to make multiple labels appear at different times C#

Time:01-23

I tried to use this code and when I pressed my button they all appeared at the same time after what I think is all the thread.sleep time combined

    private void guna2Button2_Click(object sender, EventArgs e)
    {
        Thread.Sleep(1000);
        label18.Text = "Step1";
        Thread.Sleep(4000);
        label28.Text = "Step2";
        Thread.Sleep(1500);
        label27.Text = "Step3";
        Thread.Sleep(6590);
        label26.Text = "Step4";
    }

CodePudding user response:

I think you want to update the label after the mentioned time. Try this:

private async void guna2Button2_Click(object sender, EventArgs e)
    {
        await Task.Delay(1000);
        label18.Text = "Step1";
        await Task.Delay(4000);
        label28.Text = "Step2";
        await Task.Delay(1500);
        label27.Text = "Step3";
        await Task.Delay(6590);
        label26.Text = "Step4";
    }

CodePudding user response:

they all appeared at the same time

When a windows forms program is running, there is by default a single thread executing all the code. That thread has a really important job; processing all the events from the user and drawing the UI. It only does those things when it is not processing your code that you write in a button click handler etc. If it's doing your code it's not doing those things. When the user clicks a button it stops doing those things and starts doing your things.

It's really important for a good user experience that you don't occupy its attention for very long - let's say you should try and keep to less than a hundred milliseconds. If your code captures its attention for too long the user will notice the UI jams/freezes and if the operating system notices it will fade the window and mark it as "not responding"

Your code as it is captures the thread for a very long time, and prevents it from going back to its normal work of drawing the UI. Thread.Sleep makes it sit doing nothing for the duration of the timeout, and it really does just sit and wait for the time to finish. Then you make a label visible, but the thread won't draw that as visible until you release it from your code and let it go back to doing its work, then you sleep it again. This basically means after 15 or so seconds your code will finally reach the end of the button click handler and the thread will go away, back to drawing your UI with a to-do list of "draw these 5 labels which were make visible whilst you were busy" so they all appear at once

It's an interesting learning point, embodied in the answer Amit posted, and the way try to do things now; when you use async, and await Task.Delay(...) this is very different to Thread.Sleep. Whenever a thread encounters await X it kicks off X as a background operation and crucially, it goes back to what it was doing before it started doing your code which in this case is drawing the UI. When the background operation is done it will be called back to pick up from where it left off.

This means those labels appear gradually, because the thread that is processing your code is spending most of its time actually not doing your code but drawing the UI so it can draw labels as visible very soon after you made them visible rather than being kept busy by your code all the way to the point where your code ends

The lesion you're learning here is fairly important for a lot of your future code. Using async/await is a good way of making better use of resources; threads are resource-expensive things to have sitting around doing nothing. By making better use of fewer threads (by making them fill their time with as many jobs as possible when they would otherwise be sitting around doing nothing) our programs scale up better. This means when you're kicking off an operation to download a 1gb file that will take 30 seconds, you should definitely do it with an asynchronous download approach, otherwise you'll jam your UI/hang your web server thread etc. in the case of a user app, it's just a bit annoying to have the Ui freeze all the time but in the case of a busy webserver, it having to start new threads to service new requests just because all its existing threads are jammed doing nothing means that it can run out of resources pretty quickly

CodePudding user response:

Try updating the label afterwards.

  •  Tags:  
  • Related