Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to call function timer on button click

how to call function timer on button click

Scheduled Pinned Locked Moved C#
tutorialhelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dimo1982
    wrote on last edited by
    #1

    Hi I`m trying to run timer on tick method after clicking the button and i dont really understand how it can be done. can anyone help please :doh: ?? for example : private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; //Run myTimer_tick } private void myTimer_Tick(object sender, EventArgs e) { //Do something here }

    M 1 Reply Last reply
    0
    • D dimo1982

      Hi I`m trying to run timer on tick method after clicking the button and i dont really understand how it can be done. can anyone help please :doh: ?? for example : private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; //Run myTimer_tick } private void myTimer_Tick(object sender, EventArgs e) { //Do something here }

      M Offline
      M Offline
      Meysam Mahfouzi
      wrote on last edited by
      #2

      I suppose you are trying to do something like this:

          private void button1\_Click(object sender, EventArgs e)
          {
              myTimer.Tick += new EventHandler(myTimer\_Tick);
              myTimer.Interval = 2000; // once every two seconds
              myTimer.Enabled = true;
          }
      
          void myTimer\_Tick(object sender, EventArgs e)
          {
              MessageBox.Show("tick");
          }
      
      D M 2 Replies Last reply
      0
      • M Meysam Mahfouzi

        I suppose you are trying to do something like this:

            private void button1\_Click(object sender, EventArgs e)
            {
                myTimer.Tick += new EventHandler(myTimer\_Tick);
                myTimer.Interval = 2000; // once every two seconds
                myTimer.Enabled = true;
            }
        
            void myTimer\_Tick(object sender, EventArgs e)
            {
                MessageBox.Show("tick");
            }
        
        D Offline
        D Offline
        dimo1982
        wrote on last edited by
        #3

        Yes indeed Thank you very much

        M 1 Reply Last reply
        0
        • M Meysam Mahfouzi

          I suppose you are trying to do something like this:

              private void button1\_Click(object sender, EventArgs e)
              {
                  myTimer.Tick += new EventHandler(myTimer\_Tick);
                  myTimer.Interval = 2000; // once every two seconds
                  myTimer.Enabled = true;
              }
          
              void myTimer\_Tick(object sender, EventArgs e)
              {
                  MessageBox.Show("tick");
              }
          
          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          you should not add the event handler in the button click event as you will get a new handler added each time you press the button. So in your example the message box will be shown the same number of times that the button has been clicked. Just need to take out the adding of the event handler and put that line in the design code, or constructor, or anywhere it will only be called once.

          Life goes very fast. Tomorrow, today is already yesterday.

          1 Reply Last reply
          0
          • D dimo1982

            Yes indeed Thank you very much

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            See my reply to the above answer if you find that it does not work as you want it too

            Life goes very fast. Tomorrow, today is already yesterday.

            N 1 Reply Last reply
            0
            • M musefan

              See my reply to the above answer if you find that it does not work as you want it too

              Life goes very fast. Tomorrow, today is already yesterday.

              N Offline
              N Offline
              nhqlbaislwfiikqraqnm
              wrote on last edited by
              #6

              I want to open Firefox ever hour! void myTimer_Tick(object sender, EventArgs e) { System.diragnostic.process.start("firefox); } static void Listen() { while (true) { try { data = new byte[1024]; received = networkStream.Read(data, 0, data.Length); if (received == 0) break; string get = Encoding.ASCII.GetString(data, 0, received); var send = get.ToByteArray(); //Console.WriteLine("Received from TCP Client: " + get); networkStream.Write(send, 0, send.Length); if (get == "rstartFirefox") timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = 3600000; // once every two seconds timer1.Enabled = true; here it doesn´t work! the code works great by a Button_Click! What can i do that it works here?

              M 1 Reply Last reply
              0
              • N nhqlbaislwfiikqraqnm

                I want to open Firefox ever hour! void myTimer_Tick(object sender, EventArgs e) { System.diragnostic.process.start("firefox); } static void Listen() { while (true) { try { data = new byte[1024]; received = networkStream.Read(data, 0, data.Length); if (received == 0) break; string get = Encoding.ASCII.GetString(data, 0, received); var send = get.ToByteArray(); //Console.WriteLine("Received from TCP Client: " + get); networkStream.Write(send, 0, send.Length); if (get == "rstartFirefox") timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = 3600000; // once every two seconds timer1.Enabled = true; here it doesn´t work! the code works great by a Button_Click! What can i do that it works here?

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                start by moving the following two lines to your constructor, and use it no where else

                timer1.Tick += new EventHandler(myTimer_Tick);
                timer1.Interval = 3600000

                then after all the code you have posted add the following line to check what going on..

                MessageBox.Show("get = " + get);

                let me know what pops up and how many times.

                Life goes very fast. Tomorrow, today is already yesterday.

                N 1 Reply Last reply
                0
                • M musefan

                  start by moving the following two lines to your constructor, and use it no where else

                  timer1.Tick += new EventHandler(myTimer_Tick);
                  timer1.Interval = 3600000

                  then after all the code you have posted add the following line to check what going on..

                  MessageBox.Show("get = " + get);

                  let me know what pops up and how many times.

                  Life goes very fast. Tomorrow, today is already yesterday.

                  N Offline
                  N Offline
                  nhqlbaislwfiikqraqnm
                  wrote on last edited by
                  #8

                  Nothing! Now i have it so! I get the massage box! Open also takes the correct value! But he don´t open the Methode myTimer_Tick! What can i do? public Form1() { InitializeComponent(); } void myTimer_Tick(object sender, EventArgs e) { mciSendString("set CDAudio door open", null, 127, (IntPtr)0); } if (get.Contains("rRegelmaessig_oeffnen")) { Messagebox.show("Hello"); string[] get_Split2 = get.Split('~'); int open = Convert.ToInt32(get_Split2[1]); timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = open * 1000; timer1.Enabled = true; }

                  M 1 Reply Last reply
                  0
                  • N nhqlbaislwfiikqraqnm

                    Nothing! Now i have it so! I get the massage box! Open also takes the correct value! But he don´t open the Methode myTimer_Tick! What can i do? public Form1() { InitializeComponent(); } void myTimer_Tick(object sender, EventArgs e) { mciSendString("set CDAudio door open", null, 127, (IntPtr)0); } if (get.Contains("rRegelmaessig_oeffnen")) { Messagebox.show("Hello"); string[] get_Split2 = get.Split('~'); int open = Convert.ToInt32(get_Split2[1]); timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = open * 1000; timer1.Enabled = true; }

                    M Offline
                    M Offline
                    musefan
                    wrote on last edited by
                    #9

                    what is the value of open? How do you know that the Tick event is not being called?

                    Life goes very fast. Tomorrow, today is already yesterday.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups