Using Timer only once

Posted by zaidwaqi on Stack Overflow See other posts from Stack Overflow or by zaidwaqi
Published on 2010-05-15T18:04:42Z Indexed on 2010/05/15 18:14 UTC
Read the original article Hit count: 260

Filed under:
|
|

Hi,

I want to use a timer only once, at 1 second after the initialization of my main form. I thought the following would have a message box saying "Hello World" just once, but actually a new message box says "Hello World" every one second.

Why so? I had put t.Stop() in the tick event. Also, do I need to dispose the timer somehow to avoid memory leakage?

        Timer t = new Timer();
        t.Interval = 1000;                
        t.Tick += delegate(System.Object o, System.EventArgs e)
                        { MessageBox.Show("Hello World"); t.Stop(); };

        t.Start();   

Please help and show if there is a better way of doing this? Thanks.

© Stack Overflow or respective owner

Related posts about winforms

Related posts about timer