读书人

如何让一个Timer执行完成之后再执行下

发布时间: 2013-11-23 10:52:51 作者: rapoo

怎么让一个Timer执行完成之后再执行下一个Timer。
System.Timers.Timer t = new System.Timers.Timer(3000);
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
t.AutoReset = true;
t.Enabled = true;


假如theout方法执行 需要10秒。但是Timer 的间隔是3秒。怎么判断Timer执行完成之后再执行。theout也不确定是几秒能完成。
[解决办法]

int inProcessing = 0;
void theout(object source, System.Timers.ElapsedEventArgs e)
{
if (Interlocked.CompareExchange(ref inProcessing, 1, 0) == 0)
{
try
{
// your code here...
}
finally
{
inProcessing = 0;
}
}
}

读书人网 >C#

热点推荐