Question

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();
Was it helpful?

Solution

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; 
}

OTHER TIPS

Sounds like you want a splash screen that is dismissed after 5 seconds.

There are quite a few resources on the web that show approaches to creating a WinForms splash screen (which is not as easy as it might sound), including

http://mrbool.com/how-to-make-a-splash-screen-in-csharp/26598

You can have a timer simply inform the splash screen after 5 seconds that loading is complete.

have your constructor call a background worker and on the work completed event load your main form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top