首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-11-23 
怎么让一个Timer执行完成之后再执行下一个Timer。System.Timers.Timer t new System.Timers.Timer(3000)

怎么让一个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;
        }
    }
}

热点排行