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 7652033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:39:41+00:00 2026-05-31T11:39:41+00:00

I use System.Threading.Timer to get accurate three minute intervals: private System.Threading.Timer ReadTimer; private System.Threading.TimerCallback

  • 0

I use System.Threading.Timer to get accurate three minute intervals:

private System.Threading.Timer ReadTimer;
        private System.Threading.TimerCallback ReadTimerCallback;
        public Form1()
        {
            InitializeComponent();
        }

        #region Working with RichTextBox and system event log for logging
        public enum LogStyle
        {
            InformationStyle = 0,
            WarningStyle = 1,
            ErrorStyle = 3
        }
        private delegate void LogTextDelegate(LogStyle logStyle, string message);
        private int NewLinesCount(string Message)
        {
            string[] strings = { Environment.NewLine };
            return Message.Split(strings, StringSplitOptions.RemoveEmptyEntries).Count();
        }
        private void AddText(LogStyle logStyle, string message)
        {
            LogBox.Select(0, 0);
            string TextToAppend = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff") + " " + message + Environment.NewLine;
            LogBox.AppendText(TextToAppend);
            LogBox.Select(LogBox.TextLength - TextToAppend.Length + NewLinesCount(TextToAppend), TextToAppend.Length);
            switch (logStyle)
            {
                case LogStyle.InformationStyle:
                    {
                        EventLog.WriteEntry(Process.GetCurrentProcess().ProcessName, TextToAppend, EventLogEntryType.Information);
                        LogBox.SelectionColor = Color.Green;
                        LogBox.Select(LogBox.TextLength, LogBox.TextLength);
                        break;
                    }
                case LogStyle.WarningStyle:
                    {
                        EventLog.WriteEntry(Process.GetCurrentProcess().ProcessName, TextToAppend, EventLogEntryType.Warning);
                        LogBox.SelectionColor = Color.DarkOrange;
                        LogBox.Select(LogBox.TextLength, LogBox.TextLength);
                        break;
                    }
                case LogStyle.ErrorStyle:
                    {
                        EventLog.WriteEntry(Process.GetCurrentProcess().ProcessName, TextToAppend, EventLogEntryType.Error);
                        LogBox.SelectionColor = Color.Red;
                        LogBox.Select(LogBox.TextLength, LogBox.TextLength);
                        break;
                    }
            }
        }
        public void LogText(LogStyle logStyle, string Message)
        {
            if (LogBox.InvokeRequired)
            {
                LogBox.Invoke(new LogTextDelegate(this.AddText), new object[] { logStyle, Message });
            }
            else
            {
                AddText(logStyle, Message);
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            LogText(LogStyle.WarningStyle, "Current thread ID = " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            LogText(LogStyle.WarningStyle, "Threads count = " + System.Diagnostics.Process.GetCurrentProcess().Threads.Count);
            AutoResetEvent ReadautoEvent = new AutoResetEvent(false);
            ReadTimerCallback = new TimerCallback(this.ShowText);
            ReadTimer = new System.Threading.Timer(ReadTimerCallback, ReadautoEvent, (long)(CurrentTime.AddMinutes(3) - DateTime.Now).TotalMilliseconds, 180000);

        }

        public void ShowText(object Object)
        {
            ReadTimer.Change((long)(CurrentTime.AddMinutes(3) - DateTime.Now).TotalMilliseconds, 180000);
            LogText(LogStyle.WarningStyle, "Now is " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff"));
            LogText(LogStyle.WarningStyle, "Now is " + "Current thread ID = " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            LogText(LogStyle.WarningStyle, "Threads count = " + System.Diagnostics.Process.GetCurrentProcess().Threads.Count);
        }

        private DateTime CurrentTime
        {
            get
            {

                DateTime now = DateTime.Now;
                DateTime val;
                val = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0)
                     .AddMinutes(((now.Minute) / 3) * 3 - now.Minute);
                return val;
            }
        }

But sometimes I see following:

14.03.2012 09:20:33.264 Current thread ID = 10
14.03.2012 09:20:33.361 Threads count = 13
14.03.2012 09:20:59.985 Now is 14.03.2012 09:20:59.977
14.03.2012 09:20:59.991 Now is Current thread ID = 12
14.03.2012 09:20:59.997 Threads count = 15
14.03.2012 09:21:00.010 Now is 14.03.2012 09:21:00.009
14.03.2012 09:21:00.019 Now is Current thread ID = 12
14.03.2012 09:21:00.025 Threads count = 17
14.03.2012 09:24:00.252 Now is 14.03.2012 09:24:00.252
14.03.2012 09:24:00.260 Now is Current thread ID = 8
14.03.2012 09:24:00.268 Threads count = 17
14.03.2012 09:27:00.331 Now is 14.03.2012 09:27:00.330
14.03.2012 09:27:00.337 Now is Current thread ID = 6
14.03.2012 09:27:00.343 Threads count = 14
14.03.2012 09:30:00.021 Now is 14.03.2012 09:30:00.021
14.03.2012 09:30:00.027 Now is Current thread ID = 8
14.03.2012 09:30:00.033 Threads count = 13
14.03.2012 09:32:59.962 Now is 14.03.2012 09:32:59.961
14.03.2012 09:32:59.968 Now is Current thread ID = 6
14.03.2012 09:32:59.974 Threads count = 13
14.03.2012 09:33:00.013 Now is 14.03.2012 09:33:00.013
14.03.2012 09:33:00.020 Now is Current thread ID = 6
14.03.2012 09:33:00.026 Threads count = 14
14.03.2012 09:35:59.891 Now is 14.03.2012 09:35:59.891
14.03.2012 09:35:59.898 Now is Current thread ID = 8
14.03.2012 09:35:59.904 Threads count = 13
14.03.2012 09:36:00.001 Now is 14.03.2012 09:35:59.999
14.03.2012 09:36:00.008 Now is 14.03.2012 09:36:00.004
14.03.2012 09:36:00.015 Now is Current thread ID = 8
14.03.2012 09:36:00.021 Now is Current thread ID = 6
14.03.2012 09:36:00.030 Threads count = 14
14.03.2012 09:36:00.035 Threads count = 14

Why does this execute twice in a three minute time interval : at 14.03.2012 09:20:59.985 and 14.03.2012 09:21:00.010?

Where I must fix this?

In CurrentTime I must add 30 seconds such

DateTime now = DateTime.Now.AddSeconds(30) or what?

  • 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-31T11:39:42+00:00Added an answer on May 31, 2026 at 11:39 am

    There are a few issues here. The biggest one I see is your CurrentTime property:

    DateTime now = DateTime.Now;
    DateTime val;
    val = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0)
                     .AddMinutes(((now.Minute) / 3) * 3 - now.Minute);
    return val;
    

    Consider what happens when now.Minute is 59, 59/3 = 19 because this is an int. The rest of your expression 59/3 * 3 - 59 results in -2. You have effectively rewound the timer back two minutes. However, since you zeroed out the seconds when you created val, you’ve really set the timer back 3 minutes. 59 is not a unique case, but I’m pulling it from your set of data points. The code that uses your CurrentTime adds 3 minutes. So you’re pretty much telling the code to fire immediately. The reason you are seeing it raised twice is because of this line in ShowText:

    `ReadTimer.Change((long)(CurrentTime.AddMinutes(3) - DateTime.Now).TotalMilliseconds, 180000);` 
    

    When your delegate handles the timer, the event is raised immediately again because now.Minute is still 59, causing your overall CurrentTime calculation to be 3 minutes less than DateTime.Now.

    If all you want to do is have a task run every three minutes, then the following is all you need:

    ReadTimer = new System.Threading.Timer(ReadTimerCallback, ReadautoEvent, 0, 180000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some DateTime variable, and I want to use System.Threading.Timer to wait until
I have a Silverlight 3 page. I use a System.Threading.Timer to perform an Asynchronous
I use System.Web.Services.WebMethodAttribute to make a public static method of an ASP.NET page callable
I would like to start a System.Threading.Timer in my application when it launches (maybe
The situation is as follows: a thread timer (from System.Threading.Timer) runs in an interval
I have an issue with the System.Timers.Timer object. I use the timer object to
I have an application that start System.Threading.Timer, then this timer every 5 seconds read
Is it possible to use system.currency. It says system.currency is inaccessible due to its
How do I use system environment variables in my project post-build events without having
I'm trying to use System.DirectoryServices in a web site project and I'm getting this

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.