开发者

How can i make my vb.net winform pause for few secs

i would like to pause and show my splash screen for few moments when t开发者_开发问答he program loads.

How can i do it in vb.net winforms...


Okay, imagine your main form is called Form1, and your expensive/slow initialization is done in Load, and your splash screen form is called Splash.

You'd want something like the following:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim StartTime = DateTime.Now
    Dim Splash = New System.Threading.Thread(AddressOf SplashThread)
    Splash.Start()

    'Do lots of initialization - you wouldn't have this sleep in the real application
    System.Threading.Thread.Sleep(10000)

    Dim EndTime = DateTime.Now
    Dim Diff = EndTime - StartTime
    If Diff.TotalSeconds < 5 Then
        'Splash hasn't been shown for very long - a little sleep is warranted.
        System.Threading.Thread.Sleep(New TimeSpan(0, 0, 5) - Diff)
    End If
    SplashForm.Invoke(New Action(AddressOf SplashForm.Close))
    Splash.Join()
End Sub

Private SplashForm As Splash

Private Sub SplashThread()
    SplashForm = New Splash()
    Application.Run(SplashForm)
End Sub


Quick and dirty solution:

  1. Show the splash screen form.
  2. Use Thread.Sleep (note that no UI updates will be done during that time, so if your user clicks somewhere, your splash screen might look ugly).
  3. Close the splash screen form.

Nice solution:

  1. Show the splash screen form (without UI elements that the user can use to close the form).
  2. Use a timer control in the form to timeout the "few moments".
  3. Close the splash screen form when the timer expires.

User-friendly solution:

  1. Show the splash screen form.
  2. Have your program do some useful work.
  3. Close the splash screen form.

Note that splash screens usually serve a purpose: They entertain the user while some work is being done by the program. If your program does not need to do initial work, the splash screen is just annoying because it wastes the user's time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜