我的问题是,我需要在按钮被按下后等待3-4秒,然后才能检查它,以下是我在button1_click下的代码:
While Not File.Exists(LastCap)
Application.DoEvents()
MsgBox("testtestetstets")
End While
PictureBox1.Load(LastCap)我认为我正在做一些非常简单的错误的事情,我不是最好的VB,因为我刚刚学习,所以任何解释都会很棒!
~谢谢
发布于 2012-11-23 06:57:29
如果您需要等待的原因是要创建文件,请尝试使用FileSystemWatcher并以响应事件的方式响应Created和Changed事件,而不是任意等待一段选定的时间。
类似于:
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发布于 2012-11-23 04:15:32
您可以使用,但不推荐使用:
Threading.Thread.Sleep(3000) 'ms这将等待3秒,但也会阻塞同一线程上的所有其他内容。如果您在表单中运行此命令,则用户界面将在等待结束之前不会做出响应。
顺便说一句:用MessageBox.Show("My message")代替MsgBox (后者来自旧的VB)。
发布于 2012-11-23 06:16:12
如果您希望窗体在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将Timer控件从工具箱拖放到窗体中。它在运行时不可见
https://stackoverflow.com/questions/13519274
复制相似问题