日期:2014-05-18  浏览次数:20989 次

如何实现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();
}
}