WindFell

Display Time Threads

Apr 5th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace DisplayTime
  5. {
  6.     class StartUp
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const int frequency = 3000;
  11.             int offset = 0;
  12.  
  13.             var DisplayTimeThread = new Thread(() =>
  14.             {
  15.                 try
  16.                 {
  17.                     Thread.CurrentThread.IsBackground = true;
  18.                     while (true)
  19.                     {
  20.                         DisplayTime(offset);
  21.                         Thread.Sleep(frequency);
  22.                     }
  23.                 }
  24.                 catch (Exception e)
  25.                 {
  26.                     //Send a detailed message to an error handler;
  27.                 }
  28.             });
  29.  
  30.             DisplayTimeThread.Priority = ThreadPriority.Normal;
  31.             DisplayTimeThread.Start();
  32.             ShowMenu();
  33.             GetOffsetFromUser(ref offset);
  34.  
  35.         }
  36.  
  37.         private static int GetOffsetFromUser(ref int offset)
  38.         {
  39.             while (true)
  40.             {
  41.                 switch (Console.ReadKey().Key)
  42.                 {
  43.                     case ConsoleKey.UpArrow:
  44.                         offset++;
  45.                         break;
  46.                     case ConsoleKey.DownArrow:
  47.                         offset--;
  48.                         break;
  49.                     case ConsoleKey.Escape:
  50.                         Environment.Exit(0);
  51.                         break;
  52.                 }
  53.             }
  54.         }
  55.  
  56.         static void DisplayTime(int offset)
  57.         {
  58.             Console.WriteLine(DateTime.UtcNow.AddHours(offset).ToString("hh:mm:ss"));
  59.         }
  60.  
  61.         static void ShowMenu()
  62.         {
  63.             Console.WriteLine("Press Up to Increase by one Hour!");
  64.             Console.WriteLine("Press Down to Decrease by one Hour!");
  65.             Console.WriteLine("Press Esc to Exit!");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment