在 VB.net 中延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13519274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delaying in VB.net
提问by Reece Tilley
my issue is i need to wait 3-4 seconds after a button has been pressed before i can check for it, here is my code under button1_click:
我的问题是我需要在按下按钮后等待 3-4 秒才能检查它,这是我在 button1_click 下的代码:
While Not File.Exists(LastCap)
Application.DoEvents()
MsgBox("testtestetstets")
End While
PictureBox1.Load(LastCap)
I think i'm doing something really simple wrong, i'm not the best at VB as i'm just learning so any explaining would be great!
我认为我在做一些非常简单的错误,我不是 VB 中的佼佼者,因为我只是在学习,所以任何解释都会很棒!
~Thanks
~谢谢
回答by Derek Tomes
If you want your form to continue to function while the 3 seconds pass, you can add a Timer control instead, with some code like this:
如果您希望您的表单在 3 秒过去后继续运行,您可以添加一个 Timer 控件,代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' set the timer
Timer1.Interval = 3000 'ms
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
'add delayed code here
'...
'...
MessageBox.Show("Delayed message...")
End Sub
Drag and drop a Timer control from your toolbox to your form. It's not visible at runtime
将 Timer 控件从工具箱拖放到窗体中。它在运行时不可见
回答by Mark Hall
If the reason you are needing to wait is for the file to be created try using a FileSystemWatcher
and respond to the Created
and Changed
Events that way you are responding to an event rather than arbitrarily waiting a select period of time.
如果你需要等待的原因是该文件使用创建的尝试FileSystemWatcher
和响应的Created
和Changed
活动方式,您正在响应的事件,而不是武断地等待时间的选择周期。
Something like:
就像是:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
FileSystemWatcher1.Path = 'Your Path Here
FileSystemWatcher1.EnableRaisingEvents = True
'Do what you need to todo to initiate the file creation
End Sub
Private Sub FileSystemWatcher1_Created(sender As Object, e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created, FileSystemWatcher1.Changed
If e.Name = LastCap Then
If (System.IO.File.Exists(e.FullPath)) Then
FileSystemWatcher1.EnableRaisingEvents = False
PictureBox1.Load(e.FullPath)
End If
End If
End Sub
回答by Mark Hall
You can use, although not recommended:
您可以使用,但不推荐使用:
Threading.Thread.Sleep(3000) 'ms
This will wait 3 seconds, but also block everything else on the same thread. If you run this in the form your user-interface will not response until the wait is over.
这将等待 3 秒,但也会阻塞同一线程上的所有其他内容。如果您以这种形式运行它,您的用户界面在等待结束之前不会响应。
just as a side note: use MessageBox.Show("My message")
instead of MsgBox
(latter is from old VB).
只是作为一个旁注:使用MessageBox.Show("My message")
而不是MsgBox
(后者来自旧的VB)。
回答by Joe Dabones
You could use this
你可以用这个
Public Sub BeLazy()
For i = 1 To 30
Threading.Thread.Sleep(100)
Application.DoEvents()
Next
End Sub
It will delay for 3 seconds.
它将延迟 3 秒。
回答by Joe Dabones
or better yet making a wait function using stop watch, this wont halt the process in the same thread like thread sleep
或者更好的是使用秒表制作等待功能,这不会像线程睡眠一样在同一线程中停止进程
' Loops for a specificied period of time (milliseconds)
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
usage
用法
wait(3000)
for 3 sec delay
延迟 3 秒