none
Timer not working? RRS feed

  • Question

  • Thank you guys for the support I have been receiving on this forum. I will continue to post about development questions due to the lack of useful tutorials. I will mark answers that work.

    I am trying to make a simple test where one text displays and 3 seconds later different text displays. I made a timer and set the interval to 100 - 1000 which should be a second I guess?

    Heres the code:

    private void Run_Click(object sender, EventArgs e) //a button
        {
             Output.Text = "Hello this is a test";
             timer1.Start();
             Output.Text = "Test successful!";
        }
    

    <g class="gr_ gr_766 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="766" id="766">Output</g> is a textbox that will output text. timer1 is the timer I am using. When I click the button it instantly shows "test successful" which means there is no delay. I changed the ticks from 100 to 1000 still no delay.

    So, what do you guys think? 


    Wednesday, June 20, 2018 5:17 AM

Answers

  • The timer reports its progress using Tick event. Double-click the timer in Form Designer. This will add the handler. Then move the line that writes the new text. The Tick event happens periodically, but you can stop it using timer1.Stop() after the first tick.

    You can also handle the event programmatically in this manner (in Run_Click):

       Output.Text = "Hello this is a test";

     

       EventHandler h = null;

       h = ( s, a ) =>

           {

              Output.Text = "Test successful!";

              timer1.Stop();

              timer1.Tick -= h;

           };

     

       timer1.Tick += h;

       timer1.Start();

     


    • Edited by Viorel_MVP Wednesday, June 20, 2018 5:45 AM
    • Marked as answer by DenverXDXDXD Wednesday, June 20, 2018 5:51 AM
    Wednesday, June 20, 2018 5:43 AM

All replies

  • The timer reports its progress using Tick event. Double-click the timer in Form Designer. This will add the handler. Then move the line that writes the new text. The Tick event happens periodically, but you can stop it using timer1.Stop() after the first tick.

    You can also handle the event programmatically in this manner (in Run_Click):

       Output.Text = "Hello this is a test";

     

       EventHandler h = null;

       h = ( s, a ) =>

           {

              Output.Text = "Test successful!";

              timer1.Stop();

              timer1.Tick -= h;

           };

     

       timer1.Tick += h;

       timer1.Start();

     


    • Edited by Viorel_MVP Wednesday, June 20, 2018 5:45 AM
    • Marked as answer by DenverXDXDXD Wednesday, June 20, 2018 5:51 AM
    Wednesday, June 20, 2018 5:43 AM
  • Thanks, It works fine now.
    Wednesday, June 20, 2018 5:52 AM