Click here to Skip to main content
11,702,130 members (62,223 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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...
Posted 2-Jul-13 1:20am
Edited 2-Jul-13 1:21am
v2
Comments
ThePhantomUpvoter at 2-Jul-13 7:23am
   
SendCommand("TEMP");
timer.Enabled = false;
ryanb31 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Why are you using a timer?
  Permalink  

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

  Print Answers RSS
0 Maciej Los 420
1 Sergey Alexandrovich Kryukov 355
2 OriginalGriff 314
3 Mika Wendelius 119
4 CHill60 105
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,507
2 CPallini 4,922
3 Maciej Los 4,371
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100