Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:48:58+00:00 2026-05-30T18:48:58+00:00

How do i reset the timer at the end of the day automatically and

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

            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:48:59+00:00Added an answer on May 30, 2026 at 6:48 pm

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, given a timestamp, how to reset the time part alone to 00:00:00
While reset and checkout have different usages most of the time, I can't see
I need to reset a MySQL Field value automatically at midnight. It is a
We have a server running a 6 day job that just reset, because Windows
When you have a cancel_handler for a timer, can you directly reference and reset
Just a quick question about the Timer class of Flex 4. I have a
I am building a form where users must select a time interval, from day
I've been banging my head against this one all day long. Time to ask
I'm currently trying to access a REST API for the first time using visual
I am using YUI reset/base, after the reset it sets the ul and li

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.