Show / Hide Table of Contents

    Class Suspender

    Class for simple work suspending. It is poosible to suspend work (Suspend()) several times, but in that case it is necessary to resume (Kros.Utils.Suspender.Resume) it the same number of times. The easiest way of suspending work is using the using block.

    Inheritance
    System.Object
    Suspender
    ConnectionHelper
    Namespace: Kros.Utils
    Assembly: Kros.Utils.dll
    Syntax
    public class Suspender
    Remarks

    It is useful for example in object initialization. During the initialization it is often necessary not to perform certain actions. Standard way is using some flag if the initialization is running. The Suspender class encapsulates the management of this flag, while it is possible to set this flag several times in succession (nested work suspending).

    Examples
            private Suspender _initSuspender = new Suspender();
    
        private void Init()
        {
            using (_initSuspender.Suspend())
            {
                // Do initialization...
            }
        }
    
        private void DoWork()
        {
            // Do general work.
    
            if (!_initSuspender.IsSuspended)
            {
                // Do work only when not initializing.
            }
        }

    Properties

    IsSuspended

    The flag, if work is (true), or not (false).

    Declaration
    public bool IsSuspended { get; }
    Property Value
    Type Description
    System.Boolean

    Methods

    ResumeCore()

    Used for specific (inherited) suspender implementations. This method is executed when the work is resumed for the last time. So it means it is executed when IsSuspended flag is changing from true to false.

    Declaration
    protected virtual void ResumeCore()
    Remarks

    Method is intended for implementing custom logic in own suspender when resuming work. It is executed only once during the last work resuming (preceding work resumings do not call ResumeCore()). The method is called after the IsSuspended flag is changed, so the value of flag while the method is executing is false.

    Suspend()

    Suspends the - sets the IsSuspended flag to true. If this method is called several times, it is necessary to call Kros.Utils.Suspender.Resume the same number of times to clear IsSuspended flag.

    Declaration
    public IDisposable Suspend()
    Returns
    Type Description
    System.IDisposable

    Returns helper object, which automatically calls Kros.Utils.Suspender.Resume when disposed of, so it is convenient to use using block.

    Examples
            private Suspender _initSuspender = new Suspender();
    
        private void Init()
        {
            using (_initSuspender.Suspend())
            {
                // Do initialization...
            }
        }
    
        private void DoWork()
        {
            // Do general work.
    
            if (!_initSuspender.IsSuspended)
            {
                // Do work only when not initializing.
            }
        }

    SuspendCore()

    Used for specific (inherited) suspender implementations. This method is executed when Suspend() is called for the first time. So it means it is executed when IsSuspended flag is changing from false to true.

    Declaration
    protected virtual void SuspendCore()
    Remarks

    Method is intended for implementing custom logic in own suspender when suspending work. It is executed only once during the first call of Suspend() (subsequent calls to Suspend() do not call SuspendCore()). The method is called before the IsSuspended flag is changed, so the value of flag while the method is executing is false.

    Back to top KROS a.s.