All I want is to create a timer!

Khrystyne

New member
Joined
Feb 5, 2006
Messages
2
Location
Texas
Programming Experience
Beginner
I'm new to .net and cannot believe how difficult it is trying to figure out how to create a timer that will display a splash screen on form load, then after a specified time load the main form. I've looked at other examples from other sites and forums and still can't understand the language enough to figure out how to apply an example to my situation. Could someone please take me by the hand and show me how this is done?? PLEASE?!:confused::(
 
for vb2003 i add a module to the project and have sub Main there (this is the startup object)
in the module i declare the two forms (startup and main)
VB.NET:
Friend Startup As New frmStartUp
Friend Main As New frmMain

then in Sub Main i simply run the first form as a thread, followed by the 2nd form as thread:
VB.NET:
Public Sub Main
  Application.Run(Startup)
  Application.Run(Main)
End Sub

now in the startup form, simply add a timer control and set it's Interval property to the length of time (in miliseconds) that you want it to run, now double click the timer control so it'll go to the code window with the timer's tick event already put in, in here simply put:
VB.NET:
Timer1.Enabled = False
Me.Close()

now in the form's load event put:
VB.NET:
Timer1.Enabled = True


and there ya go, it'll show the splash screen (startup form) for however long then the splash screen will disappear and the main form (actual program) will show and run

for examples on other splash screen approaches, simply do a search on this forum (search is available in the top bar on every page here)
 
Another great way to make your own timer is to cause the the thread or another thread working as the timer to sleep or whatever. Here's an example.
Class frmSplash

Sub frmSplash_Load.....
dim T as new System.Threading.Thread(addressOf Sleeper)
t.start
End Sub

Private Sub Sleeper()
System.Threading.Thread.Sleep(5000) 1000 = 1 second, so this is 5 secs
me.close()
End Sub

End Class
 
Thank you JuggaloBrotha, I'm going to try this out right now, I hope it works!

ImDaFrEaK, this looks like the type of answers I've been seeing in the past. I'm a newbie and can't understand where, how or why to place what you suggested. I'm afraid you assumed I knew more than I really do, sorry. :(
 
No problem there. Let me take a moment of my time explain a little deeper so that you do understand these answers. The reason is, it's very important an excitingly flexible tool to multithread.

If you take the time to read this it will give you a good 101 lesson on multi-threading and you will understand how this is used as a timer in some cases.

First let's talk threads.
If you are not used to multi-thread programming then chances are your program runs in your mind on one thread. This meaning that everyline of code runs in the order you program it to. You understand that when you call a sub routine the code runs the routine then returns to finish where you were. Example:
Sub Main.....
call sub Add.....' the code stops here and runs sub Add then returns
..... more code that runs after sub Add
End Sub
Sub Add()
End Sub

Now, that is all on 1 thread. But, what if you want to run sub Add and continue the current sub Main at the same time? You would start a new thread to run sub Add. WHen this happens Sub Add and Sub main run at virtually the exact same time. Example:

Sub Main.....
Call Sub Add w/a seperate thread.....' this code runs
......more code.... since Sub Add is a seperate thread this code
runs simultaniously with sub Add and dosn't wait for it to finish.
This means this sub Main could finish before sub Add.
End Sub
Sub Add()
End Sub

Now, If I havn't lost you let me share how you declare and start a thread. Here is the hiearchy to gain access to threads. System.Threading
Here is how you declare a new thread. It will become a variable just like if we were declaring a string, integer, class, ect.... I will use the variable t to stand for thread.

Dim t as System.threading.thread

Now we need to assign a sub routine to t sorta like an event handler. Basically when you start the thread, where does the code for the thread begin. This must be a Sub routine w/o arguments. Example:

Sub Add()
End Sub

This is a valid Sub to assign to t... this would not be valid.

Sub Add(ByVal x as integer)
End Sub

This is how you assign the Sub Add to t.

t = New System.Threading.Thread(addressOf Add)

The addressOf operator simply points t to a sub, the next value will be the sub name. (addressOf Add) Remember, Add is our sub routine.

Now t is in memory as a thread and is assigned the sub routine Sub Add. Now all we do is start that thread. This is simple.

t.start()

This starts the thread. Same as calling the Sub Add except now the rest of the code dosn't wait on add to finish.
You next question is probably how you use this as a timer... Please let me explain some more... b/c this is really too simple.

You have the ability to make any thread sleep or become static for a specified amount of time. This includes the main thread. In order to do that you just type...

System.Threading.Thread.Sleep(amount of time goes here)

The amount of time is an integer value with 1000 representing 1 second. So you can get a thread to sleep up from 1/thousanth of a second, 1, or one second, 1000, or more say maybe 10 seconds, 10000. This will cause the current thread to sleep 10 seconds.

System.Threading.Thread.Sleep(10000)

Use your imagination to form loops and timers and whatever you want. Here's the idea I presente earlier on this forum and I will walk you through once more.

Form1_Load.....'this is the load routine for your splash form.
Dim t as New System.Threading.Thread(addressOf Pause)
t.start() 'this starts the thread "t" at sub routine "Pause"
.....whatever else code you want here. Remember the Pause
sub routine is running right now as we do this code. Let's
take a look at the routine Pause to see what it's doing.
End Sub
Sub Pause()
System.Threading.Thread.Sleep(10000) ' the first thing the sub Pause
is doing is sleeping for 10 secs.
Me.Close() 'after it has slept for 10 seconds, it awakes only to close
the form.
End Sub

That's all there is to it and it may seem tuff but it's actually much simply to use than timers in my opinion and you can do tons more with it than simply run it as a timer. Please e-mail me if you have any questions or if I lost you in anyway. I really hope this made since and that you learned somthing from it. :)
 
Back
Top