This article shows how to create a Timer using multithreading in a console application.
The following is the procedure.
First you need to install Visual Studio 2010.
Then open Visual Studio and select "New Project...".
![]()
After opening New Projects you see this window and use the following procedure.
![]()
After Visual Studio opens on the program platform write your C# code for the console application.
![]()
You visit the Visual Studio programming platform and write there the code for the program of the console based application.
Example:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
-
- namespace Clock_MultiThreading
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Welcome to Timer Clock Multithread Program\t By Krishna Singh");
- Console.SetCursorPosition(03, 08);
- Console.Write("12 hour clock in ");
- Console.SetCursorPosition(20, 08);
- Console.WriteLine("hr:min:sec:M.Sec");
- Console.Beep();
- for (int hr = 0; hr <= 11; hr++)
- {
- for (int min = 0; min <= 59; min++)
- {
- for (int sec = 0; sec <= 59; sec++)
- {
- for (int Msec = 0; Msec < 99; Msec++)
- {
- if (hr < 10)
- {
- if (min < 10)
- {
- if (sec < 10)
- {
- Console.SetCursorPosition(18, 10);
- Console.Write("[ 0{0}: 0{1}: 0{2}:{3} ]", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- else if (sec >= 10)
- {
- Console.SetCursorPosition(18, 10);
- Console.Write("[ 0{0}: 0{1}: {2}:{3} ]", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- }
- else if (min >= 10)
- {
- if (sec < 10)
- {
- Console.SetCursorPosition(18, 10);
- Console.Write("[ 0{0}: {1}: 0{2}:{3} ]", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- else if (sec >= 10)
- {
- Console.SetCursorPosition(18, 10);
- Console.Write("[ 0{0}: {1}: {2}:{3} ]", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- }
- }
- else if (hr >= 10)
- {
- if (min < 10)
- {
- if (sec < 10)
- {
- Console.SetCursorPosition(20, 10);
- Console.Write("{0}:0{1}:0{2}:{3}", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- else if (sec >= 10)
- {
- Console.SetCursorPosition(20, 10);
- Console.Write("{0}:0{1}:{2}:{3}", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- }
- else if (min >= 10)
- {
- if (sec < 10)
- {
- Console.SetCursorPosition(20, 10);
- Console.Write("{0}:{1}:0{2}:{3}", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- else if (sec >= 10)
- {
- Console.SetCursorPosition(20, 10);
- Console.Write("{0}:{1}:{2}:{3}", hr, min, sec, Msec);
- Thread.Sleep(10);
- }
- }
- }
- }
- Console.Beep();
- }
- Console.Beep(); Console.Beep();
- }
- }
- Console.ReadKey();
- }
- }
- }
The following is the output:
![]()
So friends, this is a small article to help you create a Timer of a multithread based program console application in C#. The next time I will provide an even more interesting article, thank you. I hope this was helpful for you. Enjoy :).Krishna Singh