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:
- Show the splash screen form.
- 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). - Close the splash screen form.
Nice solution:
- Show the splash screen form (without UI elements that the user can use to close the form).
- Use a timer control in the form to timeout the "few moments".
- Close the splash screen form when the timer expires.
User-friendly solution:
- Show the splash screen form.
- Have your program do some useful work.
- 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.
精彩评论