开发者

Waiting in vb2008 / vb.net

I am a VB6 coder and I'm making the move to VB8 / VB.NET

I know how to wait in VB6, but my problem is that I don't know how to wait in VB8/VB.NET. I have a TextBox called textbox2 that contains the number of seconds I want to w开发者_JS百科ait. I used to use wait 60 in VB6, but of course VB2008 is different.

Can anyone help me to do this?


I know this is an old question, but I thought there were so many conflicting answers and I thought the solution I use is simple and straightforward enough. Also, I wrote this when I switched from VB6 to .net, for the same reason as 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


Use this do that your UI does not hang.

For i = 1 to 300
 threading.thread.sleep(i * 1000)
 application.doevents
next


[edit: I re-read the question and see it was specifically asking about a TextBox named textbox2 so I've updated the answer to reflect that.]

Well, I think one answer would be to use:

System.Threading.Thread.Sleep(Int32.Parse(textbox2.Text) * 1000);

if your text box contains the number of seconds to wait. However if you aren't in a background thread, this will hang your application up for the amount of time you are waiting.

You could also do something like:

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

which would not hang up the UI while you wait. (Also, you could use Convert.Int32(textbox2.Text) to convert the data in the textbox.)

Oh, and in certain cases, another way you can avoid the issues with the UI locking up would be do implement a timer callback instead. (see http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx for some more detail.) To do this, you

  • do whatever processing you need to do before the pause
  • create a function that picks up processing where you left off
  • create a timer that calls your function afterward

Code:

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 Class


try to use a timer

  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim interval As Integer = 0

    If Integer.TryParse(Me.TextBox2.Text, interval) Then
        Timer1.Enabled = True
        Timer1.Interval = interval
        Timer1.Start
    End If
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'your code here
End Sub

In the 'event of the timer ticks then implements the logic to invoke the method Timer.Stop (), but this depends on what you do.

Regards.


Use Thread.Sleep:

Thread.Sleep(60000)

Update, following comment:

To retrieve and convert the value of the text box use:

Dim sleepValue As Integer = Integer.Parse(textbox2.Text)

This will throw an exception if the value cannot be converted.


I don't know why do you want it and and why you don't use threads, but this sleep function act similar wait in vb6:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MsgBox("begin")
    Sleep(2000)
    MsgBox("end")
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜