Amazon.com Widgets Delayed Automatic Exit from Console Application v2.0

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

Code Project Associate Logo

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


Delayed Automatic Exit from Console Application v2.0

This is an alternate version to the one originally posted here. It occurred to me that if someone was actually at the console and the program quit without warning, while allegedly waiting for a key press, that would be a bit surprising and/or annoying. This new version displays a warning with an on screen count down leading up to the automatic exit:

 

    1 using System;

    2 using System.Threading;

    3 

    4 namespace ConsoleApplication1

    5 {

    6     class Program

    7     {

    8         static void Main(string[] args)

    9         {

   10             //... code here for whatever your console app does

   11             Console.WriteLine("[Result of the Program]" + Environment.NewLine);

   12 

   13             Console.WriteLine("...press any key to exit." + Environment.NewLine);

   14 

   15             delay = new ExitDelay();

   16             delay.Start();

   17             MyTimer = new Timer(TimesUp, null, 1000, 1000);

   18         }

   19 

   20         static ExitDelay delay;

   21         static Timer MyTimer;

   22         static int CountDown = 10;

   23 

   24         // Timer callback: they didn't press any key, but we don't want this window open forever!

   25         private static void TimesUp(object state)

   26         {

   27             if(CountDown > 0)

   28             {

   29                 Console.CursorLeft = 0;

   30                 Console.Write("Program will exit automatically in " + CountDown + " ");

   31                 CountDown--;

   32             }

   33             else

   34             {

   35                 Console.CursorLeft = 0;

   36                 Console.WriteLine("Exiting - Bye!                                   ");

   37                 Thread.Sleep(1000);

   38                 delay.Stop();

   39                 MyTimer.Dispose();

   40                 Environment.Exit(0);

   41             }

   42         }

   43 

   44     }

   45 

   46     public class ExitDelay

   47     {

   48         private readonly Thread workerThread;

   49 

   50         public ExitDelay()

   51         {

   52             this.workerThread = new Thread(this.work);

   53             this.workerThread.Priority = ThreadPriority.Lowest;

   54             this.workerThread.Name = "ExitTimer";

   55         }

   56 

   57         public void Start()

   58         {

   59             this.workerThread.Start();

   60         }

   61 

   62         public void Stop()

   63         {

   64             this.workerThread.Abort();

   65         }

   66 

   67         private void work()

   68         {

   69             Console.ReadKey();

   70             this.Stop();

   71         }

   72     }

   73 

   74 }

 


Categories: VB
Posted by Williarob on Saturday, December 13, 2008 12:08 PM
Permalink | Comments (0) | Post RSSRSS comment feed
blog comments powered by Disqus