Introduction: Make a Timer Using C#

This instructable will teach you how to make a functional timer that takes minutes, hours, and seconds!

Step 1: Step 1: Download and Install Microsoft Visual Studio

It is not that complicated! Downloading Visual Studio is fast, and it's a very good IDE to use for programming!

Step 2: Open Visual Studio, and Start a New C# Console Application Project

This part tells your computer what the type of program will be! It's simple, once you open VS, just click new project!

Step 3: ADD SYSTEM.WINDOWS.FORMS

This is probably the most essential step. If you don't add system.windows.forms the program will not work. Search Forms in the search bar after right clicking >references>add reference

Step 4: Add System.Media As Well

System.Media is also a necessity, just type using System.Media;

DON'T FORGET: you have to type using system.windows.forms AS WELL!

Step 5: Declare Your _or Variable

This variable is a simple console.readline(); that tells the computer what type of time measurement you will be using!

Step 6: Test Your Variable to Make Sure It Worked

Just press f5, but remember, if you don't have a console.read on the end, your program will close immediatley!

Step 7: Declare Your MakeNoise Function

If you didn't get any error messages on the last step, this one should be easy. Remember using system.media? well this function is why we needed it.

Once your function is declared, fill it with the while loop, thread.sleep, and noise function.

Step 8: Start Your Conditionals

These statements will tell us what to do depending on what the user writes for the _or variable. Here, you also have to define another variable that asks the user for how many of the time measurement!

Step 9: Turn MakeNoise Into the Thread: MakeNoise

Declare a thread, threads run while the Main function is doing it's thing, not one at a time, so this means that our timer will go off until we stop it

Step 10: Declare for Loops.

These loops use the number of (hours, minutes, seconds) you gave the computer to wait the program until it's time for the timer to go off.

Step 11: Start Your Thread, Wait, Stop Your Thread

This is another crucial part, these threads will run until they are aborted using thread.abort. So you have to make the computer wait for input while the threads run.

Step 12: Test Your Program

IF NO ERRORS REGISTER YOU ARE FINE. IF YOU GET EXCEPTION ERRORS THERE IS LIKELY SOMETHING WRONG WITH YOUR FOR LOOP!!! If you didn't get any errors or exceptions, move on to the next step.

Step 13: Copy the Waiting Function

Once you get the program to work with no errors, make else if statements and copy your seconds code, turn it into hours and minutes code. Just change the Thread.Sleep(1000) to Thread.Sleep(60000) and Thread.Sleep(360000)!

Step 14: Your Program Is Done!

There you have it folks! A fully functional, and very exact timer, that you just created! Thanks for reading my instructable, hope I helped!

Comments

author
mrasmussen8 made it!(author)2015-04-07

Thread.Sleep method is not really a timer - but more just the entire program freezing. . very unresponsive.

I think for an actual timer - make something that ticks, like a dispatchertimer.

For async tasks use task.delay to delay action running in threadpool :)

good 'ible on making your first programs wait though

author
Jermlain made it!(author)2015-04-07

mrasmussen8 I see what you mean, but Thread.Sleep() is not the timer, the For loop is just looping through Thread.Sleep() x times. Thank you for your constructive criticism, I'll look into task.delay. Again, thanks!

author
tomatoskins made it!(author)2015-04-07

I have such a love/hate relationship with C#. Great instructable!

About This Instructable

3,053views

23favorites

License:

More by Jermlain:Make a timer using C#
Add instructable to: