我在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'.
答案 0 :(得分:7)
您是否尝试实施启动画面? - > @ChrisDunaway是的,我正在做一个闪屏。
进入“项目属性”并将主窗体保留为“启动”窗体。将启动画面窗体设置为底部的启动画面条目。现在单击右侧的“查看应用程序事件”按钮并覆盖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
有关详细信息,请参阅here。
答案 1 :(得分:3)
如果你想在5秒后执行剩下的代码,为什么不创建一个单独的线程/任务,它将等待5秒,然后通过回调到主线程来触发其余的代码运行?这种方法不会挂起你的用户界面。
编辑:如果您需要启动画面,请删除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
。
答案 2 :(得分: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
答案 3 :(得分: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
答案 4 :(得分: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