Delay Function

HadesAnon

New member
Joined
Feb 18, 2012
Messages
1
Programming Experience
3-5
Hello.
I've been trying to code a simple thing in many ways, but they all failed. I want to make a Function that will be used as a delay ( No, I don't want System.Threading.Thread.Sleep() ). Take a look at the code:
VB.NET:
Public Function Time()        Timer.Enabled = True
        Timer.Start()
        MsgBox("Timer has started")
        Timer.Stop()
        MsgBox("Timer has stopped")
        Timer.Enabled = False
    End Function

I want the timer to start at Timer.Start(), delay up to certain seconds, and stop at Timer.Stop(). So, when it's called, I want a delay of the specified seconds.

VB.NET:
Public Function NameOfTheFunc()        CurrentStatus.Text = "Working..."
        For Me.CurrentNumber = 1 To CustomNumber.Text
            ----Do things----
            Time() ----Delay 10 secs----
            ----Do more things----
        Next NameOfTheFunc
    End Function

Help.
 
You'll also want to avoid naming your method (function or sub) with keywords that are already used by .NET.... 'Time' is one of those keywords.
 
You'll also want to avoid naming your method (function or sub) with keywords that are already used by .NET.... 'Time' is one of those keywords.
Although VB programming language doesn't have a keyword called 'Time'.
HadesAnon said:
I want to make a Function that will be used as a delay ( No, I don't want System.Threading.Thread.Sleep()
Generally, in event driven applications a timer is used for 'delay'.
 
Very good point John - TimeOfDay was what I was thinking of... advice still stands though.
 
Very good point John - TimeOfDay was what I was thinking of... advice still stands though.
TimeOfDay is a shared property in the Microsoft.VisualBasic namespace. Nothing prevents you from using the same name as any property, method or type as one of the thousands of names already used somewhere in the vast .Net class libraries. A keyword on the other hand, is something the compiler will prevent you from using, because keywords are reserved and can't be used as identifiers, unless you escape them, see Keywords (Visual Basic)
 
That's right and my use of keyword was poorly chosen here - however you surely don't condone advising someone that it's good practice to use an existing name in a user created method, do you?

Actually, no need to answer that - we've hijacked the OPs thread for long enough.
 
That's right and my use of keyword was poorly chosen here - however you surely don't condone advising someone that it's good practice to use an existing name in a user created method, do you?

Actually, no need to answer that - we've hijacked the OPs thread for long enough.
If there was no conflict ('name clash') I see no problem in using a word that was previously used elsewhere in the book, to be blunt. It would depend on context and need. Simple arrangements like types and namespaces makes member name clashes nearly nonexistent, type names may more often conflict and one have to be more careful towards especially .Net classes, if not only for coding convenience, but namespace usage can resolve those also. Remember also many parties create many libraries, it is impossible to account for any known and unknown names being used today and in future, namespace recommendations does address that.
HadesAnon said:
----Do things----
Time() ----Delay 10 secs----
----Do more things----
To further expand on my reply to OP, if an event model would not suit you, the kind of pseudo code you presented here is something you would not do on UI thread. You must not tie up UI thread for a longer time (several seconds), instead you must do such tasks on a secondary thread - where if needed it would also not be a problem to sleep the thread for the amounted time.
 
Using a Timer object with events would be MUCH better, but ...

Dim T As System.Threading.Thread = New System.Threading.Thread(AddressOf PollSomething) : T.Start

Do : Loop

Sub PollSomething()
     Do
          ' Do something
          Thread.Sleep(10000)
          ' Do something else 10 seconds later
     Loop
End Sub
 
Back
Top