c++ /CLI system . timers .当设置Interval为100ms时,计时器以秒为单位经过20秒



我在c++/CLI代码中使用定时器System::Timers::Timer,希望每100ms发生一次。但它会在20秒后触发。但是我用c#写了同样的代码,它工作得很好。

c++代码:

#include "stdafx.h"
using namespace System;
using namespace System::Timers;
using namespace System::Threading;
static void SysTick(Object^ state, ElapsedEventArgs^ e)
{
    DateTime _current = System::DateTime::Now;
    String^ dt_str = String::Format("NOW: @{0}.{1:000}", _current.ToLongTimeString(), _current.Millisecond);
    Console::WriteLine(dt_str);
}
int main(array<System::String ^> ^args)
{
    System::Timers::Timer^ sys_tick = gcnew System::Timers::Timer(100);
    sys_tick->BeginInit();
    sys_tick->AutoReset = true;
    sys_tick->Elapsed += gcnew ElapsedEventHandler(SysTick);
    sys_tick->EndInit();
    sys_tick->Start();
    for (int i = 0; i < 100; i++)
    {
        Console::WriteLine("[{0}] Main thread sleep 1000 ms", i);
        Thread::Sleep(1000);
    }
    return 0;
}
c#代码:

using System;
using System.Timers;
using System.Threading;
namespace CSharpTimer
{
    class Program
    {
        static void SysTick(object state, ElapsedEventArgs e)
        {
            DateTime now = DateTime.Now;
            Console.WriteLine("NOW: {0}.{1:000}", now.ToLongTimeString(), now.Millisecond);
        }
        static void Main(string[] args)
        {
            System.Timers.Timer timer = new System.Timers.Timer(100);
            timer.BeginInit();
            timer.AutoReset = true;
            timer.Elapsed += new ElapsedEventHandler(SysTick);
            timer.EndInit();
            timer.Start();
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("[{0}] Main thread sleep 1000 ms", i);
                Thread.Sleep(1000);
            }
        }
    }
}
c++输出:

[0] Main thread sleep 1000 ms
NOW: @5:59:58 PM.284
[1] Main thread sleep 1000 ms
[2] Main thread sleep 1000 ms
[3] Main thread sleep 1000 ms
[4] Main thread sleep 1000 ms
[5] Main thread sleep 1000 ms
[6] Main thread sleep 1000 ms
[7] Main thread sleep 1000 ms
[8] Main thread sleep 1000 ms
[9] Main thread sleep 1000 ms
[10] Main thread sleep 1000 ms
[11] Main thread sleep 1000 ms
[12] Main thread sleep 1000 ms
[13] Main thread sleep 1000 ms
[14] Main thread sleep 1000 ms
[15] Main thread sleep 1000 ms
[16] Main thread sleep 1000 ms
[17] Main thread sleep 1000 ms
[18] Main thread sleep 1000 ms
[19] Main thread sleep 1000 ms
[20] Main thread sleep 1000 ms
NOW: @6:00:18 PM.421
[21] Main thread sleep 1000 ms
[22] Main thread sleep 1000 ms
c#输出:

[0] Main thread sleep 1000 ms
NOW: 6:01:23 PM.032
NOW: 6:01:23 PM.134
NOW: 6:01:23 PM.242
NOW: 6:01:23 PM.352
NOW: 6:01:23 PM.460
NOW: 6:01:23 PM.575
NOW: 6:01:23 PM.682
NOW: 6:01:23 PM.790
NOW: 6:01:23 PM.899
[1] Main thread sleep 1000 ms
NOW: 6:01:24 PM.007
NOW: 6:01:24 PM.122
NOW: 6:01:24 PM.231
NOW: 6:01:24 PM.339
NOW: 6:01:24 PM.447
NOW: 6:01:24 PM.554
NOW: 6:01:24 PM.663
NOW: 6:01:24 PM.780
NOW: 6:01:24 PM.888
[2] Main thread sleep 1000 ms
NOW: 6:01:24 PM.995
NOW: 6:01:25 PM.104
NOW: 6:01:25 PM.212
NOW: 6:01:25 PM.320

是我遇到了这个问题。我的朋友Filly Hsu在这里问了这个问题。对这个奇怪的问题没有任何解决办法,我的朋友们的不满意的投票损失了两分。

我在MSDN上问了同样的问题,几个小时后,一位会员给了我一个解决方案:

Console::Write()后放一个Debug::Write("")

现在它解决了,虽然我仍然不知道为什么。

最新更新