yonghan79
yonghan79

Reputation: 15

vb6 function to vb.net

i got this code in vb6

Private Sub pause(ByVal interval As Variant)
    Dim Current As Variant

    Current = Timer
    Do While Timer - Current < Val(interval)
        DoEvents
    Loop
End Sub

How do I convert in vb.net? I have tried this way but it doesn't works.

Where is the mistake?

Private Sub pause(ByVal interval As Single)            
      Dim newDate As Date    
      newDate = DateAndTime.Now.AddSeconds(interval)

      While DateAndTime.Now.Second <> newDate.Second                
          Application.DoEvents()           
      End While    
End Sub

Thanks

Upvotes: 0

Views: 232

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27322

I think the following is the best conversion of the original code (subtly different to andygrips answer):

Private Sub Pause(ByVal seconds As Integer)
    Dim current As Date

    current = DateTime.Now
    Do While (DateTime.Now - current).TotalSeconds < seconds
        Application.DoEvents()
    Loop
End Sub

Note however that the use of DoEvents in .NET is best avoided altogether

Upvotes: 0

andygjp
andygjp

Reputation: 2464

It will only escape the While if the Now seconds equal newDate seconds. Try this instead:

    Private Sub pause(ByVal interval As Single)
       Dim waitUntil As Date = DateAndTime.Now.AddSeconds(interval)
       While DateAndTime.Now < waitUntil
           Application.DoEvents()
       End While
    End Sub

Also can I suggest that you name your methods using PascalCase (the first letter of the method name is capitalised).

Upvotes: 1

Related Questions