Things I have learned today – Console Apps Using a Timer and CurrentThread!!!

So today I was creating a console application using timers in Visual Basic.Net. It happened to be a really simple, very tiny program. It consisted of two timers (of the System.Timers.Timer variety). All the main function was doing was calling these two timers. One catch to this, this kind of timer is asynchronous (runs on a separate thread from the main function).
I needed the program to wait while these timers (separate threads finished). After thinking about it for a while and after some trial and error here is what I came up with:

System.Threading.CurrentThread.Join(time in milliseconds)

It completely rocks! It will not spike the CPU at all! You will spike the CPU if you were to do something like:
Dim isFinished as Boolean
timer1.Start()

While Not isFinished
End While

CurrentThread is awesome! You can quickly manipulate your program to be have in ways you couldn’t even imagine.

Enjoy!

Advertisements