日期:2014-05-17  浏览次数:21215 次

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