1

我需要一个适合我的 Visual Basic 中的等待命令。

我知道:

Declare Sub Sleep Lib "kernel32.dll" (ByVal milliseconds As Long)
sleep 5000

但这会使程序无响应。

System.Threading.Thread.Sleep(5000) 'The window doesn't load until the timing is over (useless)

我的代码:

Imports Microsoft.Win32 'To check if is 64Bit or 32Bit

Public Class Loading
  Private Sub Loading_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("x86") Then
      My.Settings.Is64 = False
    Else
      My.Settings.Is64 = True
    End If

    'I need command here

    If My.Settings.Is64 = True Then
      Form64.Show()
      Me.Close()
    Else
      MsgBox("No version developed for 32-bit computers.")
      End
    End If
  End Sub
End Class

错误:

@Idle_Mind

1. function 'OnInitialize' cannot be declared 'Overrides' because it does not override a function in a base class.
2.  'MinimumSplashScreenDisplayTime' is not a member of 'App.Loading.MyApplication'.
    3.  'OnInitialize' is not a member of 'Object'.
4

5 回答 5

7

从评论:

在此处输入图像描述

进入项目属性并将您的主窗体保留为启动窗体。将启动屏幕表单设置为底部的启动屏幕条目。现在单击右侧的“查看应用程序事件”按钮并覆盖OnIntialize,以便您可以像这样设置MinimumSplashScreenDisplayTime()

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
            ' Set the display time to 5000 milliseconds (5 seconds). 
            Me.MinimumSplashScreenDisplayTime = 5000
            Return MyBase.OnInitialize(commandLineArgs)
        End Function

    End Class


End Namespace
于 2013-07-12T14:31:07.173 回答
3

如果您想在 5 秒后执行其余代码,为什么不创建一个单独的线程/任务,它会等待 5 秒,然后通过对主线程的回调触发其余代码运行?这种方法不会挂起您的 UI。

编辑:如果你想要一个启动画面,放下一个 Timer 控件,将间隔设置为 5 秒,然后在 Tick 事件处理程序中运行其余代码。

假设您已经设置了 Timer,请将您的加载代码移动到Timer1_Tick处理程序中:

Public Class Loading
  Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'part 1
    If Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\0").GetValue("Identifier").ToString.Contains("x86") Then
      My.Settings.Is64 = False
    Else
      My.Settings.Is64 = True
    End If

    'part 2
    If My.Settings.Is64 = True Then
      Form64.Show()
      Me.Close()
    Else
      MsgBox("No version developed for 32-bit computers.")
      End
    End If
  End Sub
End Class

或将第 1 部分留在 中Load,将第 2 部分移入Tick。对于语义,我更喜欢这个选项。

也不要忘记设置Timer.Enabled = True

于 2013-07-12T14:12:20.730 回答
2

如果你想要一个地方来取消应用程序,你可以使用Application.Startup事件并e.Cancel = True从那里设置。完成后,主窗体甚至不会出现;应用程序将简单地退出。这可能看起来像:

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If someCondition Then
                MessageBox.Show("oops")
                e.Cancel = True ' <-- main form will NOT show, app will simply exit
            End If
        End Sub

    End Class


End Namespace
于 2013-07-12T15:03:03.223 回答
1

这样做:

    For i As Integer = 1 To 500
        System.Threading.Thread.Sleep(10)
        System.Windows.Forms.Application.DoEvents()
    Next

编辑:小心 DoEvents;如果用户单击某些内容或处理不应该处理的事件,则可能会导致问题。见 http://www.codinghorror.com/blog/2004/12/is-doevents-evil.html

于 2013-07-12T13:25:35.853 回答
0

由于睡觉和忙着等待通常是不受欢迎的,你可以做一些事情AutoResetEvent

Private ReadOnly _resetEvent As AutoResetEvent = New AutoResetEvent(False)

Sub Pause(ByVal milliseconds As Long)
    Dim waitInterval As TimeSpan = TimeSpan.FromMilliseconds(milliseconds)
    While Not _resetEvent.WaitOne(waitInterval)
         ' Waiting!
    End While
End Sub

不建议这样做,但这里有一个使用System.Diagnostics.Stopwatch类的示例:

Sub Pause(ByVal milliseconds As Long)
    If milliseconds <= 0 Then Return
    Dim sw As New Stopwatch()
    sw.Start()
    Dim i As Long = 0
    Do
        If i Mod 50000 = 0 Then ' Check the timer every 50,000th iteration
            sw.Stop()
            If sw.ElapsedMilliseconds >= milliseconds Then
                Exit Do
            Else
                sw.Start()
            End If
        End If
        i += 1
    Loop
End Sub

然后在需要暂停的地方,调用这个Pause()方法:

Pause(5000) ' Pause for 5 seconds
于 2013-07-12T13:23:17.113 回答