C# .net定期执行一个方法
当我单击一个按钮执行名为C# .net定期执行一个方法,c#,.net,timer,C#,.net,Timer,当我单击一个按钮执行名为sendData()的方法时,它工作正常。 我需要在应用程序启动时定期执行它 private void btnSend_Click(object sender, EventArgs e) { SendData(); } private void SendData() { //some code and logic } 我将代码更改为以下内容,但不起作用,不会显示错误消息: public partial class someForm : Form {
sendData()
的方法时,它工作正常。
我需要在应用程序启动时定期执行它
private void btnSend_Click(object sender, EventArgs e)
{
SendData();
}
private void SendData()
{
//some code and logic
}
我将代码更改为以下内容,但不起作用,不会显示错误消息:
public partial class someForm : Form
{
private System.Windows.Forms.Timer mtimer;
public someForm()
{
mtimer = new System.Windows.Forms.Timer();
mtimer.Interval = 100; //1ms
mtimer.Tick += new EventHandler(OnReadTimer);
}
private void OnReadTimer(object sender, EventArgs eArgs)
{
if (sender == mtimer)
{
SendData();
}
}
private void SendData() {}
您需要调用该方法以使计时器开始计时
你从不启动计时器。(mtimer.Start())
mtimer = new System.Windows.Forms.Timer();
mtimer.Interval = 100; //1ms
mtimer.Tick += new EventHandler(OnReadTimer);
mtimer.Start()