Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Timer in C#

Tags:

c#

timer

I'm trying to make a form invisible for x amount of time in c#. Any ideas?

Thanks, Jon

like image 877
Jon Avatar asked Jan 05 '09 00:01

Jon


2 Answers

BFree has posted similar code in the time it took me to test this, but here's my attempt:

this.Hide();
var t = new System.Windows.Forms.Timer
{
    Interval = 3000 // however long you want to hide for
};
t.Tick += (x, y) => { t.Enabled = false; this.Show(); };
t.Enabled = true;
like image 159
Matt Hamilton Avatar answered Nov 16 '22 23:11

Matt Hamilton


Quick and dirty solution taking advantage of closures. No Timer required!

private void Invisibilize(TimeSpan Duration)
    {
        (new System.Threading.Thread(() => { 
            this.Invoke(new MethodInvoker(this.Hide));
            System.Threading.Thread.Sleep(Duration); 
            this.Invoke(new MethodInvoker(this.Show)); 
            })).Start();
    }

Example:

// Makes form invisible for 5 seconds.

Invisibilize(new TimeSpan(0, 0, 5));
like image 30
Robert Venables Avatar answered Nov 16 '22 23:11

Robert Venables