0

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

Luis
  • 5,786
  • 8
  • 43
  • 62
yonghan79
  • 15
  • 6

2 Answers2

1

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).

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
andygjp
  • 2,464
  • 1
  • 16
  • 11
  • According to the microsoft standard naming conventions for .NET, private members should be in camelCase, and public members should be in PascalCase. – Steven Doggart Jul 12 '12 at 11:57
  • I don't believe Microsoft actually say that you should use camelCase for private members, only that you should use PascalCase for public members. I use PascalCase for all methods because I find it more readable. – andygjp Jul 12 '12 at 12:37
  • @andygjp - There are Pros and cons to all naming conventions but in the end resistance is futile! – Matt Wilko Jul 12 '12 at 13:00
  • Ah, yup, you're right, the official guidelines don't specify the correct casing for private members. So, it's left up to each developer to decide on their own naming convention for those. – Steven Doggart Jul 12 '12 at 13:00
0

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

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • @yonghan79 You need to have a look into multi threading - check out the [BackGroundWorker class](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) – Matt Wilko Jul 12 '12 at 15:17
  • Thanks,i will try it.By the way do you mind to take a look at my code,i'll send the code if it's ok with you. :) I have tried the converted sub,it gives me an error,is it possible caused by Application.DoEvents? – yonghan79 Jul 12 '12 at 15:54
  • Sorry if i just reply now,i have been away for a while.It shows me mscrolib.dll error,but then i add Threading.Thread.Sleep(1000),it works.Thanks for you help. :) – yonghan79 Jul 17 '12 at 12:36
  • How are you?Can i ask for your help again?It still doesn't work as i expected.I post the code here [CODE](http://pastebin.com/zeycXgFv).I use a timer with interval=500.It gives me like this Photobucket.Could you help me pointing where is the mistake?Thanks. – yonghan79 Jul 19 '12 at 01:58
  • @yonghan79 You need to start a new question posting the code and the error you are getting, that way you will get a better response – Matt Wilko Jul 19 '12 at 08:08