简体   繁体   English

每x秒调用一次方法的最佳实践是哪种?

[英]Which is the best practice to call a method every x seconds?

I have a program that controls and communicate with a microprocessor. 我有一个控制并与微处理器通信的程序。 The program sends commands to the microprocessor and receives the feedback at the same time the command is sent showing the same in various text boxes.This is activated/fired by the command control. 程序将命令发送到微处理器,并在发送命令的同时接收反馈,并在各个文本框中显示该命令。此命令由命令控件激活/触发。 I need now to implement some feedback that is not related to the command and therefore is not activated by the user, (current and temperature measurement) but is relevant the status of the machine controlled by the micro. 现在,我需要实现一些与命令无关的反馈,因此该反馈不会被用户激活(电流和温度测量),但与微控制器控制的机器状态相关。 I do not like the idea to time the message to be sent on the micro side, I would rather receive the information and process the same every x seconds on the pc side. 我不喜欢将消息的发送时间安排在微端的想法,我宁愿接收信息并在PC端每隔x秒处理一次。 In this way I can also include a selector for different update intervals. 这样,我还可以包括一个用于不同更新间隔的选择器。 I am controlling a motor and a heater. 我正在控制电机和加热器。

The question is: which is the best practice to call the relevant classes to process such messages( current and temperature), timer, new thread with timer, backgroundworker,something else? 问题是:哪种最佳方法是调用相关类来处理此类消息(当前和温度),计时器,带有计时器的新线程,backgroundworker等。

Interface: I recommend you create something like startPoll() and endPoll() inside your controller class: This is the only place, where all necessary information to avoid multiple-call, interleaving etc. are in scope. 接口:我建议您在控制器类中创建诸如startPoll()endPoll()类的东西:这是唯一的地方,在这里所有避免重复调用,交织等必要信息都在范围内。

Implementation: I recommend spinning off a dedicated thread: The thread pool and other built-in mechanisms are not designed for work, that has a high degree of independance from the rest of the app (such as communicating with other hardware). 实现:我建议分离一个专用线程:线程池和其他内置机制不是为工作而设计的,它们与应用程序的其余部分具有高度的独立性(例如与其他硬件进行通信)。 This thread could be started by startPoll() and signalled to end by endPoll() . 这个线程可以通过启动startPoll()并暗示将结束endPoll()

Timer: I definitly do NOT recommend using a timer - you go straight into parallel-call-hell, if one check takes longer than the wait for the next check (or you need plumbing code to avoid that). 计时器:我绝对不建议您使用计时器-如果一个检查的时间比等待下一个检查的时间长(或者您需要管道代码来避免这种情况),则直接进入并行呼叫地狱。 I recommend you calculate the ealiest next start time before you start your work cycle, then when finished WaitOne() on the stop signal for the remaining time. 我建议您在开始工作周期之前,然后在停止信号上的WaitOne()完成之后,计算最短的下一次开始时间,以得出剩余时间。

I guess a timer will solve it.. But not any timer, but the one in System.Threading namespace. 我想一个计时器可以解决它。但是,不是任何计时器,而是System.Threading命名空间中的计时器。

http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

Following your advices I have implemented this code: 按照您的建议,我已实现此代码:

        public Form1()
        {
        InitializeComponent();
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        timer.Tick += OnTimerTick;
        timer.Interval = 5000;
        timer.Start();
        }
        private void OnTimerTick(object sender, EventArgs e)
        {
        // I will fire the events here
         MessageBox.Show("5 seconds elapsed");
        }

the code is obviously working but I am not sure if this is in line with your advices. 该代码显然可以正常工作,但是我不确定这是否符合您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM