Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms wait 5 seconds before displaying a message

I want to make the user wait for 5 seconds before the user can do something but I'm having trouble as I don't want to do Thread.Sleep(5000); as I want the form to be loaded and the functionality to be be viewable but I don't want to allow the user to do anything for those 5 seconds (well they can attempt to click buttons but nothing should happen).

What I did to make this work (my code is slightly different due to properties) thanks to the answerer:

var t = Task.Delay(1000) //1 second/1000 ms
t.Wait();
like image 858
Zain Avatar asked Jul 23 '26 15:07

Zain


1 Answers

Well you can always disable all form and after 5 seconds enable it...

(example using .net framework 4.5)

//Your window Constructor
public MyWindow()
{
    InitializeComponent();

    this.Cursor = Cursors.WaitCursor; 
    this.Enabled = false;
    WaitSomeTime();

    //load stuff
    .....
}

public async void WaitSomeTime()
{
    await Task.Delay(5000);
    this.Enabled = true;
    this.Cursor = Cursors.Default; 
}
like image 158
Sílvio N. Avatar answered Jul 25 '26 06:07

Sílvio N.