loading screen in c#

Viewed 74

I'm trying to use a loading gif image for a loading screen in windows form I want the loading screen to show up for 5 seconds I am trying to implement that with thread.sleep() function but the problem was everything stops in the current winForm includes the gif image itself. I want to delay the 5 seconds of loading time with the animated gif image working

2 Answers

Thread.Sleep() blocks the current thread (ie. the UI thread). You can use this wait method which uses a timer.

public void wait(int milliseconds)
{
    var timer1 = new System.Windows.Forms.Timer();
    if (milliseconds == 0 || milliseconds < 0) return;

    timer1.Interval = milliseconds;
    timer1.Enabled = true;
    timer1.Start();

    timer1.Tick += (s, e) =>
    {
        timer1.Enabled = false;
        timer1.Stop();
    };

    while (timer1.Enabled)
    {
        Application.DoEvents();
    }
}
Related