WaitEx() Function :D

Discussion in 'Programming General' started by Blupig, Dec 27, 2008.

WaitEx() Function :D
  1. Unread #1 - Dec 27, 2008 at 1:04 PM
  2. Blupig
    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant

    Blupig BEEF TOILET
    $5 USD Donor

    WaitEx() Function :D

    I'm learning VB.net so I decided to translate a couple easier things from VB6. This was a popular Wait() function used a lot in VB6, so I thought I could make it better AND put it into VB.net, so here we are. There is an easier way to do this, but since I'm learning I thought I'd give it a go ;)

    Code:
        Public Declare Function timeGetTime Lib "winmm.dll" () As Long
    
        Public Sub WaitEx(ByVal value As Long, ByVal seconds As Boolean)
    
            On Error Resume Next
            Dim cTime As Long
    
            cTime = timeGetTime()
    
            If seconds = True Then
    
                Do
                    Application.DoEvents()
                Loop While cTime + (value * 1000) > timeGetTime()
    
            Else
    
                Do
                    Application.DoEvents()
                Loop While cTime + value > timeGetTime()
    
            End If
    
        End Sub
    How to use:
    Value property (long): This is the time that the application will wait for.
    Seconds property (boolean): Set to true to have the application wait for [value] in seconds, set to false to have it wait for [value] in milliseconds.
     
  3. Unread #2 - Dec 27, 2008 at 5:05 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    WaitEx() Function :D

    Shorten it? :D
    Code:
     Public Declare Function timeGetTime Lib "winmm.dll" () As Long
    
        Public Sub WaitEx(ByVal value As Long, ByVal seconds As Boolean)
    
            On Error Resume Next
            Dim cTime As Long
    
            cTime = timeGetTime()
    
            If seconds = True Then
    
                value = value * 1000
    
            End if
    
            Do
                Application.DoEvents()
            Loop While cTime + value > timeGetTime()
    
        End Sub
     
  5. Unread #3 - Dec 27, 2008 at 8:33 PM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    WaitEx() Function :D

    Made slightly better. "On Error Resume Next" Is SO Visual Basic 6.

    Code:
        Public Declare Function timeGetTime Lib "winmm.dll" () As Long
    
        Public Sub WaitEx(ByVal value As Long, ByVal seconds As Boolean)
            Dim cTime As Long
    
            Try
                cTime = timeGetTime()
            
                If seconds = True Then
    
                    value = value * 1000
    
                End if
    
                Do
                    Application.DoEvents()
                Loop While cTime + value > timeGetTime()
    
    
            Catch Ex As Exception
                Console.WriteLine(Ex.Message)
                Console.WriteLine(Ex.StackTrace)
            End Try
        End Sub
     
  7. Unread #4 - Dec 27, 2008 at 8:53 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    WaitEx() Function :D

    I figured .Net must have implemented the Try Catch but I don't do much of it so good job thinking of it. Team effort. :)
     
  9. Unread #5 - Dec 27, 2008 at 8:53 PM
  10. Flaming Idiots
    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User

    Flaming Idiots Active Member
    Visual Basic Programmers

    WaitEx() Function :D

    Why use a native function to get the time? This should work just fine, just use 0.3 for 300 milliseconds.
    Code:
    Public Sub Wait(ByVal Seconds As Double)
        If Seconds <= 0 Then Throw New InvalidOperationException("Must wait more than zero seconds.")
        Dim EndTime = Now + TimeSpan.FromSeconds(Seconds)
        Do While Now <= EndTime
            System.Windows.Forms.Application.DoEvents()
        Loop
    End Sub
    
    It would be better to use a timer or an alternate thread though.
     
  11. Unread #6 - Dec 27, 2008 at 9:31 PM
  12. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    WaitEx() Function :D

    I use it in almost every program that I write ^_^
     
< need help with phpbb | [Help] Sockets Problem >

Users viewing this thread
1 guest


 
 
Adblock breaks this site