在C#中使用Timer

Jon*_*Jon 7 c# timer

我试图在c#中使表单在x时间内不可见.有任何想法吗?

谢谢,乔恩

Mat*_*ton 16

BFree在我测试时发布了类似的代码,但这是我的尝试:

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;
Run Code Online (Sandbox Code Playgroud)


Rob*_*les 8

快速而肮脏的解决方案利用封闭.无需定时器!

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();
    }
Run Code Online (Sandbox Code Playgroud)

例:

//使表单隐藏5秒钟

Invisibilize(new TimeSpan(0,0,5));