C# how to use different timer with different intervals, but start and stop them at the same time

  • Thread starter Thread starter zydjohn
  • Start date Start date
Z

zydjohn

Guest
Hello:

I have to use 2 timers and start and stop them at the same time, having one timer with interval of 1 second, another timer with interval of 2 seconds. The following is my C# (.NET Core) code:

using System;
using System.Threading;

namespace TimerTask2
{
class Program
{
static void Main()
{
AutoResetEvent autoEvent1 = new AutoResetEvent(false);
StatusChecker1 statusChecker1 = new StatusChecker1(10);
TimerCallback timerDelegate1 =
new TimerCallback(statusChecker1.CheckStatus);
TimeSpan delayTime1 = new TimeSpan(0, 0, 1);
TimeSpan intervalTime1 = new TimeSpan(0, 0, 0, 0, 1000);
Console.WriteLine("{0} Creating timer1.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer1 = new Timer(
timerDelegate1, autoEvent1, delayTime1, intervalTime1);
autoEvent1.WaitOne(10000, false);
stateTimer1.Dispose();
Console.WriteLine("\nDestroying stateTimer1.");

AutoResetEvent autoEvent2 = new AutoResetEvent(false);
StatusChecker2 statusChecker2 = new StatusChecker2(5);
TimerCallback timerDelegate2 =
new TimerCallback(statusChecker2.CheckStatus);
TimeSpan delayTime2 = new TimeSpan(0, 0, 2);
TimeSpan intervalTime2 = new TimeSpan(0, 0, 0, 0, 2000);
Console.WriteLine("{0} Creating timer2.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer2 = new Timer(
timerDelegate2, autoEvent2, delayTime2, intervalTime2);
autoEvent2.WaitOne(10000, false);
stateTimer2.Dispose();
Console.WriteLine("\nDestroying stateTimer2.");
}
}

class StatusChecker1
{
int invokeCount, maxCount;

public StatusChecker1(int count)
{
invokeCount = 0;
maxCount = count;
}

public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent1 = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if (invokeCount == maxCount)
{
invokeCount = 0;
autoEvent1.Set();
}
}
}

class StatusChecker2
{
int invokeCount, maxCount;

public StatusChecker2(int count)
{
invokeCount = 0;
maxCount = count;
}

public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent2 = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {2,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if (invokeCount == maxCount)
{
invokeCount = 0;
autoEvent2.Set();
}
}
}
}

My code can get compiled, but when run, it has the following error:

System.FormatException
HResult=0x80131537
Message=Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Source=System.Private.CoreLib
StackTrace:
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.IO.TextWriter.WriteLine(String format, Object arg0, Object arg1)
at System.Console.WriteLine(String format, Object arg0, Object arg1)
at TimerTask2.StatusChecker2.CheckStatus(Object stateInfo) in C:\Test\Helper\TimerTask2\TimerTask2\Program.cs:line 77
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

Besides, it seems the first timer runs, then the second timer runs.

I want the both to run at the same time, and stop at the same time.

Is there any way to do this?

Thanks,

PS: My IDE: Visual Studio 2019 (Version 16.1.5) on Windows 10 (Version 1903).

Continue reading...
 
Back
Top