Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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...
Posted
Updated 2-Jul-13 1:21am
v2
Comments
[no name] 2-Jul-13 7:23am    
SendCommand("TEMP");
timer.Enabled = false;
ZurdoDev 2-Jul-13 7:57am    
You need to disable your timer. But if you are only sending it once why not when your app starts instead of using a timer?

1 solution

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


Why are you using a timer?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900