Make a Timer Using C#

by Jermlain in Circuits > Software

6140 Views, 24 Favorites, 0 Comments

Make a Timer Using C#

Screenshot (16).png

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

Step 1: Download and Install Microsoft Visual Studio

Screenshot (19).png

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

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

Screenshot (1).png

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

ADD SYSTEM.WINDOWS.FORMS

Screenshot (2).png

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

Add System.Media As Well

Screenshot (3).png

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

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

Declare Your _or Variable

Screenshot (4).png

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

Test Your Variable to Make Sure It Worked

Screenshot (5).png

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

Declare Your MakeNoise Function

Screenshot (6).png
Screenshot (7).png

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.

Start Your Conditionals

Screenshot (8).png

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!

Turn MakeNoise Into the Thread: MakeNoise

Screenshot (9).png

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

Declare for Loops.

Screenshot (10).png

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.

Start Your Thread, Wait, Stop Your Thread

Screenshot (11).png

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.

Test Your Program

Screenshot (12).png

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.

Copy the Waiting Function

Screenshot (14).png

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)!

Your Program Is Done!

Screenshot (17).png

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