junPac
junPac

Reputation: 111

using do-while and skipping the next lines

I want to run a method _doing() that loops infinitely until a shutdownEvent is triggered. This is basically executed/started on a new Thread(). But I need not _doSomething if it is true somewhere. what should i do after if (_doSomething)? Code snippet below. Thanks.

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");
}

Upvotes: 0

Views: 37

Answers (2)

junPac
junPac

Reputation: 111

Ohw! I just realized that a do-while is similar to a while. And to 'skip' the execution, you just have to use the continue; if the if statement is true

Upvotes: 0

Lunster
Lunster

Reputation: 906

You could use the continue; statement.

Upvotes: 2

Related Questions