使用do-while并跳过下一行

时间:2014-11-07 01:13:59

标签: c# .net do-while

我想运行一个无限循环的方法_doing(),直到触发shutdownEvent为止。这基本上是在new Thread()上执行/启动的。但如果某个地方_doSomething,我不需要true。我应该在if (_doSomething)之后做些什么?下面的代码片段。感谢。

private HighResolutionLapseTimer _lapseTimer;

private int _timeToNext
{
    get
    {
        int lapseTime = _lapseTimer.LapseTime();
        int next = DO_PERIOD - lapseTime;

        if (next > 0)
        {
            return next;
        }
        else
        {
            return 1;
        }
    }
}

int DO_PERIOD = 60000;

private void _doing()
{
    int _nextDoing = DO_PERIOD;
    Thread.Sleep(_nextDoing);

    do
    {
        LogInfo("Starting _doing");
        lock (this)
        {
            if (_doSomething)
            {
                // skip this time because we are currently archiving
            }
            _doSomething = true;
        }

        try
        {
            _lapseTimer.Start();
            DoSomethingHere(); //takes long processing
        }
        catch (Exception ex)
        {
            LogException(ex);
        }
        finally
        {
            lock (this)
            {
                _nextDoing = (int)_timeToNext;
                _doSomething = false;
            }
        }
    } while (!shutdownWaitHandle.WaitOne(_nextDoing, false));

    LogInfo("Stopping _doing");
}

2 个答案:

答案 0 :(得分:2)

您可以使用continue;言。

答案 1 :(得分:0)

OHW!我刚刚意识到do-while类似于while。要“跳过”执行,如果continue;为真,您只需使用if statement