使用do while并跳过下一行

本文关键字:一行 do while 使用 | 更新日期: 2023-09-27 18:29:34

我想运行一个无限循环的方法_doing(),直到触发shutdownEvent。这基本上是在new Thread()上执行/启动的。但如果某个地方是true,我就不需要_doSomethingif (_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");
}

使用do while并跳过下一行

您可以使用continue;陈述

哦!我刚刚意识到do-while类似于while。要"跳过"执行,只需使用continue;(如果if statement为真)