如何运行获取数据(10ms),绘制图表(10ms)和保存数据(10ms)同步的3个任务?

时间:2019-06-13 08:05:10

标签: c# multithreading timer

我有2个计时器,通过rs485获取数据,并以10ms的速度无法显示到listView,

private void tmr1_Tick()
{
    ReadMessages();
}
private void tmr2_Tick()
{
    DisplayMessages();
}

然后我用[DllImport("winmm.dll")]创建了准确的10毫秒来保存数据。 准确时间的类别是

    internal sealed class TimePeriod : IDisposable
    {
        private delegate void TimerEventDel(int id, int msg, IntPtr user, int dw1, int dw2);
        private const int TIME_PERIODIC = 1;
        private const int EVENT_TYPE = TIME_PERIODIC;// + 0x100;  // TIME_KILL_SYNCHRONOUS causes a hang ?!
        [DllImport("winmm.dll")]
        private static extern int timeBeginPeriod(int msec);
        [DllImport("winmm.dll")]
        private static extern int timeEndPeriod(int msec);
        [DllImport("winmm.dll")]
        private static extern int timeSetEvent(int delay, int resolution, TimerEventDel handler, IntPtr user, int eventType);
        [DllImport("winmm.dll")]
        private static extern int timeKillEvent(int id);    


    public void AccurateTimer(Form form, Action action, int delay)
    {
        mAction = action;
        mForm = form;
        timeBeginPeriod(1);
        mHandler = new TimerEventDel(TimerCallback);
        mTimerId = timeSetEvent(delay, 0, mHandler, IntPtr.Zero, EVENT_TYPE);
    }
}
mTimer1 = new TimePeriod();
mTimer1.AccurateTimer(this, new Action(WritetoTextFile), 10);

但是,如果我使用AccurateTimer运行ReadMessages();,我的代码将因错误消息callbackoncollecteddelegate而崩溃。

如何通过多线程和10ms获得3个任务? 谢谢!

0 个答案:

没有答案