我是一名VB6程序员,我正在转向VB8 / VB.NET
我知道如何在VB6中等待,但我的问题是我不知道如何在VB8/VB.NET中等待。我有一个名为textbox2的TextBox,它包含了我想要等待的秒数。我过去常常在VB6中使用wait 60,但VB2008当然不同。
有人能帮我做这件事吗?
发布于 2012-10-23 05:37:51
我知道这是一个古老的问题,但我认为有太多相互矛盾的答案,我认为我使用的解决方案足够简单和直接。此外,我在从VB6切换到.net时写了这篇文章,原因与OP相同。
Private Sub Wait(ByVal seconds As Long)
Dim dtEndTime As DateTime = DateTime.Now.AddSeconds(seconds)
While DateTime.Now < dtEndTime
Application.DoEvents()
End While
End Sub发布于 2011-08-21 20:37:11
使用Thread.Sleep
Thread.Sleep(60000)更新,如下评论:
要检索和转换文本框的值,请使用:
Dim sleepValue As Integer = Integer.Parse(textbox2.Text)如果值不能转换,这将抛出异常。
发布于 2011-08-21 20:38:12
编辑:我重新阅读了这个问题,看到它特别询问了一个名为textbox2的TextBox,所以我更新了答案以反映这一点。
好吧,我认为一个答案是使用:
System.Threading.Thread.Sleep(Int32.Parse(textbox2.Text) * 1000);如果文本框中包含等待的秒数,则返回。但是,如果您不在后台线程中,这将使您的应用程序在您等待的时间内挂起。
你也可以这样做:
Dim StartTime As DateTime
StartTime = DateTime.Now
While (DateTime.Now - StartTime) < TimeSpan.FromSeconds(Int32.Parse(textbox2.Text))
System.Threading.Thread.Sleep(500)
Application.DoEvents()
End While它不会在您等待时挂起UI。(您还可以使用Convert.Int32(textbox2.Text)来转换文本框中的数据。)
哦,在某些情况下,另一种避免UI锁定问题的方法是实现一个计时器回调。(有关更多详细信息,请参见http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx。)要执行此操作,您需要
代码:
Public Class MyClass
Public MyTimer As System.Timers.Timer
Public Sub OnWaitCompleted(source As Object, e As ElapsedEventArgs)
MyTimer.Stop()
MyTimer = Nothing
DoSecondPartOfProcessing()
End Sub
Public Sub DoFirstPartOfProcessing()
' do what you need to do before the wait
MyTimer = New System.Timers.Timer(Int32.Parse(textbox2.Text))
AddHandler MyTimer.Elapsed, AddressOf OnWaitCompleted
MyTimer.Start()
End Sub
Public Sub DoSecondPartOfProcessing()
' do what you need to do after the wait
End Sub
End Classhttps://stackoverflow.com/questions/7138261
复制相似问题