// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Threading; namespace E { public class CountDown { private const int UNIT = 10000; private int ms; private bool abort; public CountDown() : this(0) { } public CountDown(int ms) { this.ms = ms; } public int Ms { get { return ms; } set { ms = value; } } public int Divisions { get { return ms / UNIT; } } public int Remainder { get { return ms % UNIT; } } public void RunMany() { while(true) { Thread t = new Thread(RunOne); abort = false; t.Start(); Console.WriteLine("Press any key to continue"); Console.ReadKey(); if(t.IsAlive) { abort = true; Console.WriteLine("Countdown aborted"); } t.Join(); } } public void RunOne() { Console.WriteLine("Countdown initiated"); for(int i = 1; i <= Divisions && !abort; i++) { Thread.Sleep(UNIT); Console.WriteLine((i * UNIT)/1000.0 + " sec has passed"); } Thread.Sleep(Remainder); Console.WriteLine("TIME IS UP!"); } } public class Program { private const int DEFAULT_MS = 60000; public static void Main(string[] args) { CountDown c = new CountDown(); if(args.Length > 0) { int msarg; if(int.TryParse(args[0], out msarg)) { c.Ms = msarg; } else { Console.WriteLine("Invalid argument - using default values"); c.Ms = DEFAULT_MS; } } else { Console.WriteLine("No argument - using default values"); c.Ms = DEFAULT_MS; } c.RunMany(); } } }