1

How do I get the count in once I stop the execution in the same page? For example, now the line keeps repeating till I stop it. It shows the count remaining when I start the process. But I need the remaining count left after I stop the repeatin line.

Someone please help me out.

The Program is -

namespace Time_Writer
{
    class Program
    {
        static int count = 1;
        static double seconds;
        static int total = 10000;
        private static System.Timers.Timer aTimer;

        static void Main(string[] args)
        {
            ReadCountFromFile();

            aTimer = new System.Timers.Timer();
            aTimer.Elapsed +=new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Interval = 5000;
            aTimer.Enabled = true;
            Console.WriteLine("Press Enter To Exit The Program\n");
            Console.ReadLine();
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);

        }
        private static void ReadCountFromFile()
        {
            try
            {
                if (File.Exists(".\\mynumber.dat"))
                {
                    using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
                    {
                        byte[] bytes = new byte[4];
                        file.Read(bytes, 0, 4);
                        count = BitConverter.ToInt32(bytes, 0);
                        total = total - count;
                        Console.WriteLine("Total count left is = {0}", total);
                        Console.WriteLine("Limit = 10000");
                        Console.WriteLine("Count  = {0}", count);
                        Console.WriteLine("Count: {0} of 10000. {1} remaining", count, 10000 - count);
                     }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem reading file.");
            }
        }
        static void CurrentDomain_ProcessExit(Object sender, EventArgs e)
        {
            using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
            {
                var buffer = BitConverter.GetBytes(count);
                file.Write(buffer, 0, buffer.Length);
            }
        }
        private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Name is Yap {0}", e.SignalTime);
            seconds += 5;
            count += 1;
            if (count>10000 || seconds == 86400)
            {
                aTimer.Enabled = false;
                Console.WriteLine("\n\nTimer is off at {0}\n\n", e.SignalTime.TimeOfDay.ToString());

            }
        }
   }
}
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
3692
  • 628
  • 2
  • 6
  • 17
  • See Fredrik's answer here: http://stackoverflow.com/questions/1119841/net-console-application-exit-event - its a full example program with an empty "message pump" running on a separate thread, essentially he uses the message pump to trap the event before the console app ProcesssExits. There are comments indicating you need "a way of gracefully quitting your application" so you may replace CurrentDomain_ProcessExit with another function that inturn calls CurrentDomain_ProcessExit after printing the result. Also see EricLaw -MSFT- answer. HTH – Jeremy Thompson Mar 06 '12 at 05:40
  • any final solution with full source code ? – Kiquenet Aug 30 '12 at 10:23

1 Answers1

0

Modify the last three lines of code as following. The application should be a console application.

 static void Main(string[] args)
        {
            ReadCountFromFile();

            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
            aTimer.Interval = 5000;
            aTimer.Enabled = true;
            Console.WriteLine("Press Enter To Exit The Program\n");
//Modification starts from here
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
            Console.ReadLine();
//Modification ends from here
        }
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92