每2秒运行一次功能

Pra*_*ash 2 c# function timer

    public void InitTimer()
    {
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 200; // in milliseconds
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("test");
    }
Run Code Online (Sandbox Code Playgroud)

使用该代码如何在.NET中每小时(或每小时的特定时间间隔)引发事件?

我对C#很新,但我不确定是什么问题.我试图每2秒在这个例子中显示一个消息框.没有错误,消息框根本不显示.

Ska*_*ami 5

你忘了在负载中调用该函数.

  • 我不会创造这样的"睡眠功能",因为失去了资源,因为auf死/睡眠线程...... (2认同)