0

How do i reset the timer at the end of the day automatically and how do i display the time and date it was executed the last time? 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);

                    }
                }
            }
            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());

            }
        }
    }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
3692
  • 628
  • 2
  • 6
  • 17
  • What do you mean by reset the timer at the end of the day? stopping your application or executing the "process exit" and restart the process once again – Guillaume Mar 02 '12 at 13:28
  • executing the process exit and restart the process once again.. – 3692 Mar 04 '12 at 07:06
  • @Guillaume And what if i have to stop my application? I also want to display the count, time and date everytime after i've completed the process and restart it the next time i run it.. – 3692 Mar 06 '12 at 04:46
  • look at this post it should solved you're problem http://stackoverflow.com/questions/4646827/on-exit-for-a-console-application – Guillaume Mar 06 '12 at 12:55

1 Answers1

0

I modify your code and wrap your timer into a thread. I reduces the timer and count as well to make it easier to test. I'm sure there is a much better way to code it but this solution seems to work. you may need to adjust the thread sleep according to your need.

You can adjust when the process should stop and restart by playing with the condition

if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now) )

in the function aTimer_Elapsed.

Currenlty the process restart if it has been running for more than 1s or the count is reach.

class Program
{

    private static DateTime _processStart;
    static int count = 1;
    const int TOTAL = 15;
    private static Timer aTimer;

    private static Thread _process;

    static void Main(string[] args)
    {
        _process = new Thread(DoProcess);
        _process.Start();
        Console.WriteLine("Press Enter To Exit The Program\n");
        Console.ReadLine();
        ProcessExit();
    }

    static void DoProcess()
    {
        _processStart = DateTime.Now;
        ReadCountFromFile();

        if (count < TOTAL)
        {
            Console.WriteLine("******START TIMER******");
            aTimer = new Timer();
            aTimer.Elapsed += aTimer_Elapsed;
            aTimer.Interval = 500;
            aTimer.Enabled = true;
            while (aTimer.Enabled)
            {
                Thread.Sleep(1000);
            }
            Console.WriteLine("******END TIMER******");
            ProcessExit();
            DoProcess();
        }
    }

    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);
                    Console.WriteLine("Total count left is = {0} / Limit = {1} / Count  = {2}", TOTAL - count, TOTAL, count);

                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Problem reading file.");
        }
    }

    static void ProcessExit()
    {
        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);
        if (count < TOTAL)
        {
            count += 1;
            Console.WriteLine("Count is {0}", count);
        }
        if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now) 
        {
            aTimer.Enabled = false;
            Console.WriteLine("Timer is off at {0} count is {1}", e.SignalTime.TimeOfDay.ToString(),count);
        }
    }
}
Guillaume
  • 1,176
  • 2
  • 11
  • 27