2011-03-08 104 views
-1

我想暂停并显示我的启动画面,当程序加载时的片刻。我如何让我的vb.net winform暂停几秒钟

我如何能做到这一点在vb.net的WinForms ...

+1

暂停将是一个非常糟糕的主意 - 你希望你的应用程序的启动是* *慢比它通常会是什么? – 2011-03-08 08:29:44

回答

1

快速和肮脏的解决方案:

  1. 显示启动画面形式。
  2. 使用Thread.Sleep(请注意,在此期间不会进行UI更新,因此如果您的用户在某处单击,您的启动画面可能看起来很难看)。
  3. 关闭启动画面表单。

尼斯溶液:

  1. 显示启动画面形式(没有用户可以用它来关闭窗体UI元素)。
  2. 在窗体中使用一个计时器控件来超时“片刻”。
  3. 定时器到期时关闭闪屏窗体。

用户友好的解决方案:

  1. 显示启动画面形式。
  2. 让你的程序做一些有用的工作。
  3. 关闭启动画面表单。

请注意,启动画面通常用于以下目的:当程序正在完成一些工作时,它们招待用户。如果你的程序不需要做初始工作,初始屏幕只是很烦人,因为它浪费了用户的时间。

+0

我喜欢定时器控件的解决方案 – KoolKabin 2011-03-18 07:30:25

2

好吧,假设你的主窗体被称为Form1,并且你的Load/Slow初始化工作是在Load中完成的,并且你的初始屏幕窗体被称为Splash。

你想要的东西类似如下:

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 
+0

对于涉及线程的解决方案。我们现在唯一缺少的是关闭闪屏的选项。 – 2011-03-08 09:09:35