首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么实现setTimeOut功能

2012-02-12 
如何实现setTimeOut功能?我一直不熟悉Timer如何用,就是喜欢JavaScript的setTimeOut. 但是我在做软件,不可

如何实现setTimeOut功能?
我一直不熟悉Timer如何用,就是喜欢JavaScript的setTimeOut. 但是我在做软件,不可能用JavaScript。我用是VS 2005,C#。


例如,有一个按钮Button1. 点击一下后,它就消失。一分钟后它就出现。


请问,如何才能实现?

谢谢!

[解决办法]
用Timer类

C# code
using System;using System.Threading;public class Example{    public static void Main()    {        // Create an instance of the Example class, and start two        // timers.        Example ex = new Example();        ex.StartTimer(2000);        ex.StartTimer(1000);        Console.WriteLine("Press Enter to end the program.");        Console.ReadLine();    }    public void StartTimer(int dueTime)    {        Timer t = new Timer(new TimerCallback(TimerProc));        t.Change(dueTime, 0);    }    private void TimerProc(object state)    {        // The state object is the Timer object.        Timer t = (Timer) state;        t.Dispose();        Console.WriteLine("The timer callback executes.");    }}
[解决办法]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.button1.Visible = false;
Timer t= new Timer();
t.Interval = 2 * 1000;
t.Tick += new EventHandler(t_Tick);
t.Start();
}

void t_Tick(object sender, EventArgs e)
{
this.button1.Visible = true;
((Timer)sender).Stop();
}
}

热点排行