How to Create Timer of Multithread Based Program on Console Application in C#

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:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Threading;  
  5.   
  6. namespace Clock_MultiThreading  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("Welcome to Timer Clock Multithread Program\t By Krishna Singh");  
  13.             Console.SetCursorPosition(03, 08);  //set the cursor position  
  14.             Console.Write("12 hour clock in ");  //set clock type 12hrs or 24hrs i set here 12 hrs clock  
  15.             Console.SetCursorPosition(20, 08);  
  16.             Console.WriteLine("hr:min:sec:M.Sec");  
  17.             Console.Beep();  
  18.             for (int hr = 0; hr <= 11; hr++)  // Apply  for loop for hour  
  19.             {  
  20.                 for (int min = 0; min <= 59; min++)    // Apply  for loop for min  
  21.                 {  
  22.                     for (int sec = 0; sec <= 59; sec++)  // Apply  for loop for sec  
  23.                     {  
  24.                         for (int Msec = 0; Msec < 99; Msec++)  // Apply  for loop for Msec  
  25.                         {  
  26.                             if (hr < 10)         //Apply if condition for hour  
  27.                             {  
  28.                                 if (min < 10)      //Apply if condition for min  
  29.                                 {  
  30.                                     if (sec < 10)    //Apply if condition for sec  
  31.                                     {  
  32.                                         Console.SetCursorPosition(18, 10);  
  33.                                         Console.Write("[ 0{0}: 0{1}: 0{2}:{3} ]", hr, min, sec, Msec);  
  34.                                         Thread.Sleep(10); //apply thread with time sleep  
  35.                                     }  
  36.                                     else if (sec >= 10)  
  37.                                     {  
  38.                                         Console.SetCursorPosition(18, 10);  
  39.                                         Console.Write("[ 0{0}: 0{1}: {2}:{3} ]", hr, min, sec, Msec);  
  40.                                         Thread.Sleep(10);  //apply thread with time sleep  
  41.                                     }  
  42.                                 }  
  43.                                 else if (min >= 10)  
  44.                                 {  
  45.                                     if (sec < 10)  
  46.                                     {  
  47.                                         Console.SetCursorPosition(18, 10);  
  48.                                         Console.Write("[ 0{0}: {1}: 0{2}:{3} ]", hr, min, sec, Msec);  
  49.                                         Thread.Sleep(10);   //apply thread with time slep  
  50.                                     }  
  51.                                     else if (sec >= 10)  
  52.                                     {  
  53.                                         Console.SetCursorPosition(18, 10);  
  54.                                         Console.Write("[ 0{0}: {1}: {2}:{3} ]", hr, min, sec, Msec);  
  55.                                         Thread.Sleep(10);   //apply thread with time sleep  
  56.                                     }  
  57.                                 }  
  58.                             }  
  59.                             else if (hr >= 10)  
  60.                             {  
  61.                                 if (min < 10)  
  62.                                 {  
  63.                                     if (sec < 10)  
  64.                                     {  
  65.                                         Console.SetCursorPosition(20, 10);  
  66.                                         Console.Write("{0}:0{1}:0{2}:{3}", hr, min, sec, Msec);  
  67.                                         Thread.Sleep(10);  //apply thread with time sleep  
  68.                                     }  
  69.                                     else if (sec >= 10)  
  70.                                     {  
  71.                                         Console.SetCursorPosition(20, 10);  
  72.                                         Console.Write("{0}:0{1}:{2}:{3}", hr, min, sec, Msec);  
  73.                                         Thread.Sleep(10);  //apply thread with time sleep  
  74.                                     }  
  75.                                 }  
  76.                                 else if (min >= 10)  
  77.                                 {  
  78.                                     if (sec < 10)  
  79.                                     {  
  80.                                         Console.SetCursorPosition(20, 10);  
  81.                                         Console.Write("{0}:{1}:0{2}:{3}", hr, min, sec, Msec);  
  82.                                         Thread.Sleep(10);  //apply thread with time sleep  
  83.                                     }  
  84.                                     else if (sec >= 10)  
  85.                                     {  
  86.                                         Console.SetCursorPosition(20, 10);  
  87.                                         Console.Write("{0}:{1}:{2}:{3}", hr, min, sec, Msec);  
  88.                                         Thread.Sleep(10);  //apply thread with time sleep  
  89.                                     }  
  90.                                 }  
  91.                             }  
  92.                         }  
  93.                         Console.Beep();  
  94.                     }  
  95.                     Console.Beep(); Console.Beep();  
  96.                 }  
  97.             }  
  98.             Console.ReadKey();  
  99.         }  
  100.     }  

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

Up Next
    Ebook Download
    View all
    Learn
    View all