【发布时间】:2011-08-02 04:55:44
【问题描述】:
我想做的是使用 System.Threading.Timer 执行一个有间隔的方法。 我的示例代码看起来像这样。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Threading.Timer t1 = new System.Threading.Timer(WriteSomething, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(10));
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
public void WriteSomething(object o)
{
textBox1.Text = "Test";
}
}
}
这不是假设每 10 秒执行一次 WriteSomething 方法吗?实际发生的是,当我运行我的应用程序并在 10 秒后应用程序关闭时执行了 WriteSomething。认为我误解了它是如何工作的,谁能告诉我如何使用 System.Threading.Timer 来做到这一点。
提前致谢,非常欢迎代码示例
【问题讨论】:
-
为什么要使用 Threading.Timer 而不是 Forms.Timer?
标签: c# methods timer intervals