如何在c#的开始时间只运行一次计时器。 [英] How to run timer only once at the starting time in c#.

查看:848
本文介绍了如何在c#的开始时间只运行一次计时器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在c#中使用计时器立即向硬件发送命令,我不想创建一个发送命令的按钮但是当我运行启动程序它必须自动发送命令,所以为此我创建了定时器来通过usb串口发送命令。但问题是它每秒发送一次所以任何人都可以帮助我在程序启动时只发送一次。我不想每秒都发送。

Hi,

I am using timer in c# to send command to hardware at once, I don't want to create a button to send command but when i run start program it has to send command automatically, so for that purpose I have created timer to send command to via usb serial port. But the problem is it is sending every second so can anyone help me how to send only once when program starts. I don't want to send every second.

public partial class FormWithTimer : Form
    {
        Timer timer = new Timer();

        public FormWithTimer()
        {
            InitializeComponent();

            timer.Tick += new EventHandler(timer_Tick); // Every Time timer ticks, timer_Tick will be called
            timer.Interval = (1000) * (1);              // Timer will tick every second
            timer.Enabled = true;                       // Enable the timer
            timer.Start();                              // Start the timer
        }

        void timer_Tick(object sender, EventArgs e)
        {
           SendCommand("TEMP");
        }
    }







有什么建议吗?



提前致谢...




Any suggestion?

Thanks in advance...

推荐答案

void timer_Tick(object sender, EventArgs e)
{
   SendCommand("TEMP");
   timer.Stop();
}





你为什么要使用计时器?



Why are you using a timer?


这篇关于如何在c#的开始时间只运行一次计时器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆