Weired behavior of Timer

OK, this is driving me nuts:

I'm changing the size of a form, when a button gets clicked. The actual changing is delayed by a timer... just for an nice effect. :-)
This works fine the first time.
When clicking another buttons, the form goes back to its original size. And clicking the first button again, it will change its size...
When the first button gets clicked the second time, the form doesn't resize as smooth, as it did the first time. Seems the like the whole form gets repainted every timer, the Timer.Tick() event gets called.
Why is this
I already tried the following:
- Using just one timer for both buttons
- Using two different timers
- Using two different event-methods

When I delete the second botton (the second effect) the resizing works fine, no matter how often I do it...

Thanks,
Finch.


Answer this question

Weired behavior of Timer

  • jrimpo

    FYI. . .
    now this may have changed as things have changed and I've been wrong before(my girlfriend keeps the list)

    The reason for the approach I outlined is that events place messages in a queue. Stopping the timer ensures that meassges posted by events that may be fired by actions within the handler get placed on the queue in proper sequence.



  • raducu

    Hi Matthew,

    thanks! Now setting the forms opacity back to 1 works fine! Thank you for that! (how did you find this out anyways )
    I'm still having trouble making the form disappear (setting its opacity to "-=30" until it is 0) doesn't work:
    When this event-method is called, the form just flashes twice and then disappears (without the effect I actually wanted).
    Got a solution for that as well :-)

    Thank you,

    Finch.

  • EltonSky

    Sounds like you need to post the code for the second button, so we can work out what's going wrong.

  • arit

    Thanks for your effort,

    I changed a view things acording to your post, but the behavior is still the same... so I guess, I'll just get rid of the two other timers, and the effects and leave it by one timer, with one effect... that works!
    Too bad...

    Thanks anyway,

    Finch.

  • sohails

    That's how I wanted it to work:
    Click on Button 1, the form increases their size

    Click on Button 2, the form first "disappears" by decreasing its opacitiy, then it resizes back to the original size, then, it appears again by increasing its opacity. Now the original form is restored. Now you can click on the first button to get the bigger size...

    If I do it like this, the behavior if the form is totally screwed up...

    private
    Timer T1;
    private Timer T2;
    private Timer T3;

    //Button 1
    this.T1 = new Timer();
    T1.Interval = 5;
    T1.Start();
    T1.Tick += new EventHandler(this.Make_big);

    //event method
    public void Make_big(object sender, EventArgs eArgs)
    {
       if(this.lengthOfForm < 744)
       {
      
       this.Size = new System.Drawing.Size(this.lengthOfForm+=30, 464);
       }
    //if
    else
     {
       this.Size = new System.Drawing.Size(this.lengthOfForm+=16, 464);
       this.T1.Stop();
     }
    //else
    }//Make_big

    //Button 2
    void B_restartClick(object sender, System.EventArgs e)
    {
    this.T2 = new Timer();
    this.T2.Interval = 10;
    this.T2.Start();
    this.T2.Tick += new EventHandler(this.decreaseOpacity);
    }

    //event Method
    public void decreaseOpacity(object source, EventArgs e)
    {
    if (this.Opacity > 0)
    {
       this.Opacity -= 0.05;
    }
    //if
    else
    {
       
    this.T2.Stop();

       this.T3 = new Timer();
       this.T3.Interval = 10;
       this.T3.Start();
       this.T3.Tick += new EventHandler(this.increaseOpacity);

       this.lengthOfForm = 174;
       this.Size = new System.Drawing.Size(174, 464);
       this.Location = new System.Drawing.Point(this.orgX, this.orgY);
       this.dataHT = LD.readDataIntoStructure();
       this.fillCombo_customer(this.dataHT);
       this.changeSize = true;
    }
    //else
    }//decreaseOpacity

    //event method
    public void increaseOpacity(object sender, EventArgs eArgs)
    {
       if (this.Opacity < 1)
       {
          
    this.Opacity += 0.05;
       }
    //if
       else
       {
          
    this.T3.Stop();
       }
    //else
    }//resetFormToStartUp


  • Mark Gilbert

    I don't know if this is addresses your issue - this is more about how to use timers.
    (I am sure there are exceptions but this is typical)

    Before you start a timer, set its handler. . .
    The first line of a timer's tick handler should stop the timer.
    The last line of a timer should restart the timer (if conditions call for it)

    And if you change the appearance of a control - refresh it. Do this before restarting the timer.


    this.T1 = new Timer();
    T1.Interval = 5
    ;
    T1.Tick += new EventHandler(this.Make_big
    );
    T1.Start
    ();

    public void Make_big(object sender, EventArgs eArgs
    )
    {
       Timer t = sender as Timer;
       if (t == nullreturn;
       t.Stop
    ();
       bool restart = this.lengthOfForm < 744;
       this.Size = new Size(this.lengthOfForm+= ( restart 30 :16), 464
    );
       this.Refresh();
       if (restart) 
          t.Start();
    }

     


    I'll leave the rest to you.


  • acelik

    There's a problem when you set opacity to anything other than 1.0 and then set it back to 1.0.
    It makes screen redraws take much longer in some cases.

    The solution for that is: After setting opacity back to 1, set the form's AllowTransparency property to false.

    I don't know if that's the problem you're having, but it's probably worth a try.

  • Weired behavior of Timer