• Log in or sign up for an account here: https://www.sythe.org/login

WaitEx() Function :D

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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.
 
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
 
Member of the Month Winner
Sythe's 10th Anniversary
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
 
Made slightly better. "On Error Resume Next" Is SO Visual Basic 6.

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. :)
 
Two Factor Authentication User
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.
 
Member of the Month Winner
Sythe's 10th Anniversary
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. :)
I use it in almost every program that I write ^_^
 
Runescape Gamblingrunescape gambling
Back
Top