73est
73est

Reputation: 185

Code not executing before thread sleep?

I do this:

clear();
coinRefundComplete.Visible = true;
state = 0;

System.Threading.Thread.Sleep(4000);
clear();

greeting.Visible = true;
rate.Visible = true;
refundTicket.Visible = true;
currentTime.Visible = true;

I expect the coinRefundComplete Text (it is a label) to appear for 4 seconds, then get cleared by a method I defined with clear(), and then some other stuff happens. Instead after I clear my form with the first clear(), my form is blank for 4 seconds, then finishes properly.

Upvotes: 3

Views: 1895

Answers (3)

RBT
RBT

Reputation: 25917

Update [22-Apr-2018]: I've not deleted this answer so that you don't go the wrong path even though my answer is indeed a possible solution to OP's question (specially when it costs me some negative reputation as well). You should read below posts to really convince yourself as to why Application.DoEvents isn't a great solution to this problem:


Your UI is not refreshing because you are doing your entire processing on UI thread so UI thread is not getting any chance to refresh the UI elements. You need to call the Application.DoEvents() function at any place where you seek UI to be refreshed and loaded with latest changes. Here is your modified code. I've added one line of code before calling sleep on the current UI thread:

        clear();
        coinRefundComplete.Visible = true;
        state = 0;

        //new line of code
        System.Windows.Forms.Application.DoEvents();

        System.Threading.Thread.Sleep(4000);
        clear();

        greeting.Visible = true;
        rate.Visible = true;
        refundTicket.Visible = true;
        currentTime.Visible = true;

Upvotes: -3

Adil
Adil

Reputation: 148130

Although putting the GUI thread to sleep is never desirable but allowing the GUI thread to refresh the control state before going to Sleep will show you the changes you want. Call Control.Update or Control.Refresh after making it visible and before going to sleep so that the GUI thread is able to show changes before it goes to sleep.

clear();
coinRefundComplete.Visible = true;
label1.Update();
state = 0;    
System.Threading.Thread.Sleep(4000);
clear();

You should be carefull while using Thread.Sleep, In your case it is GUI thread and GUI will be irresponsive for the time you sleep. Knowing the reason why you want to block thread could bring some other better suggestion.

Edit

You can use other thread for adding delay without blocking the GUI thread. If you can use framework 4.5 then you can use async / await construct or read this article Using async/await without .NET Framework 4.5 .

private async void testAsyncAwaitDely_Click(object sender, EventArgs e)
{
     clear();
     coinRefundComplete.Visible = true;
     state = 0;
     await Task.Delay(4000);
     clear();
     //Your code
}

Upvotes: 0

Fabio
Fabio

Reputation: 32455

Use async/await approach.
Make your method async - below example for eventhandler of button click

private async void ButtonClick(object sender, EventArgs e)
{
    clear();
    coinRefundComplete.Visible = true;
    state = 0;

    await Task.Delay(4000);
    clear();

    greeting.Visible = true;
    rate.Visible = true;
    refundTicket.Visible = true;
    currentTime.Visible = true;
}

On line await Task.Delay(4000); UI thread will be release, which will update all changes were made before. After 4 seconds method will continue executing on the UI thread.

Upvotes: 11

Related Questions