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