如何返回定时器函数在c#中运行了多少次



所以我正在写一个c#程序,它有一个简单的定时器函数,像这样开始

static void TimerMethod(object source, ElapsedEventArgs e)
{

//Insert function code here.

}

我想知道,有没有一种方法来返回多少次定时器函数已经运行到一个字符串?谢谢。

将计数保留在类级别变量中:

class YourClass{
int _timerFireCounter = 0;

void TimerMethod(object source, ElapsedEventArgs e)
{
_timerFireCounter++;
}

void HowManyTimerFiresButton_Click(object source, EventArgs e){
MessageBox.Show($"The timer has fired {_timerFireCounter} time{(_timerFireCounter==1?"":"s")}");
}

最新更新