protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed)
            {
                Stop();

                // Protect against recursion (eg linked disposables could create a cycle)
                _startableState = StartableState.Disposed;

                if (disposing)
                {
                    // Dispose all the linked disposables
                    foreach (var disposable in _linkedDisposables)
                    {
                        if (disposable != null)
                        {
                            try
                            {
                                disposable.Dispose();
                            }
                            catch (ObjectDisposedException)
                            {} // No need to log this
                            catch (Exception exception)
                            {
                                var tracer = SetupTracerFactory.TracerFor(this);
                                tracer.Error(exception, "Exception while disposing " + disposable + ".");
                            }
                        }
                    }
                    _linkedDisposables.Clear();
                }
            }
        }
Example #2
0
        /// @inheritdoc
        public virtual void Start()
        {
            lock (this)
            {
                if (IsStarted || (_startableState == StartableState.Starting))
                { // Do nothing if already started or starting.
                    return;
                }
                if (_startableState >= StartableState.Disposing)
                { // Do nothing if already started.
                    throw new ObjectDisposedException(this.ToString(), "Cannot be started; state is: " + _startableState);
                }
                if (!ReadyToStart)
                {
                    throw new LogJamStartException(this + " cannot be started; state is: " + _startableState, this);
                }
                _startableState = StartableState.Starting;
            }

            try
            {
                InternalStart();
                _startableState = StartableState.Started;
            }
            catch (LogJamStartException)
            {
                _startableState = StartableState.FailedToStart;
                throw;
            }
            catch (Exception exception)
            {
                _startableState = StartableState.FailedToStart;
                throw new LogJamStartException("Exception starting " + this, exception, this);
            }
        }
Example #3
0
            public void Stop()
            {
                lock (this)
                {
                    var thread = _thread;
                    if (thread != null)
                    {
                        _startableState = StartableState.Stopping;
                        thread.Join();
                    }

                    _startableState = StartableState.Stopped;
                }
            }
Example #4
0
        private void Run()
        {
            computationInfo.IsActive = true;
            State = StartableState.Running;

            try
            {
                var results = new List <GymResult <Exercisetype, Resulttype> >();

                double runs = athlitis.Count * dataset.Count;
                computationInfo.Length = 1.0 / runs;
                double counter = 0;
                foreach (var athlit in athlitis)
                {
                    athlit.ComputationInfo = ComputationInfo;
                    var res = new GymResult <Exercisetype, Resulttype>();
                    res.Athlete = athlit;
                    results.Add(res);
                    foreach (var exercise in dataset)
                    {
                        if (State == StartableState.CancelRequested)
                        {
                            PublishResults.Enter(results);
                            computationInfo.IsActive = false;
                            State = StartableState.Online;
                            return;
                        }
                        var startTime = DateTime.Now;
                        var result    = athlit.execute(exercise, ResultEvaluation);
                        result.StartTime = startTime;
                        result.EndTime   = DateTime.Now;
                        result.Challenge = exercise;
                        res.ExerciseResults.Add(result);
                        counter++;
                        computationInfo.From = counter / runs;
                    }
                }
                PublishResults.Enter(results);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                computationInfo.IsActive = false;
                State = StartableState.Online;
            }
        }
        /// <summary>
        /// Stops this instance; closes all disposables.
        /// </summary>
        public void Stop()
        {
            if (IsStopped || (_startableState == StartableState.Unstarted) || (_startableState == StartableState.Stopping))
            {
                return;
            }

            var    tracer    = SetupTracerFactory.TracerFor(this);
            string className = GetType().Name;

            tracer.Info("Stopping " + className + "...");

            try
            {
                _startableState = StartableState.Stopping;
                InternalStop();
                _startableState = StartableState.Stopped;
            }
            catch (Exception stopException)
            {
                _startException = stopException;
                tracer.Error(stopException, "Stop failed: Exception occurred.");
                _startableState = StartableState.FailedToStop;
            }

            foreach (var disposableRef in _disposeOnStop)
            {
                IDisposable disposable = disposableRef.Target as IDisposable;
                if (disposable != null)
                {
                    try
                    {
                        disposable.Dispose();
                    }
                    catch (ObjectDisposedException)
                    {} // No need to log this
                    catch (Exception exception)
                    {
                        tracer.Error(exception, "Exception while disposing " + disposable + ".");
                    }
                }
            }
            _disposeOnStop.Clear();

            tracer.Info(className + " stopped.");
        }
Example #6
0
            public void Start()
            {
                lock (this)
                {
                    if (IsStarted)
                    {
                        return;
                    }

                    Debug.Assert(!IsThreadRunning);

                    _startableState = StartableState.Starting;
                    _thread         = new Thread(BackgroundThreadProc)
                    {
                        Name = "BackgroundMultiLogWriter.BackgroundThread"
                    };
                    _thread.Start();
                }
            }
Example #7
0
        private void checkState()
        {
            if (State == StartableState.Running)
            {
                return;
            }

            var at  = (athlitis.NotNullOrEmpty());
            var ex  = dataset.NotNullOrEmpty();
            var res = ResultEvaluation != null;

            if (at && ex && res)
            {
                State = StartableState.Online;
            }
            else
            {
                State = StartableState.Offline;
            }
        }
Example #8
0
        /// @inheritdoc
        public void Stop()
        {
            lock (this)
            {
                if (!IsStarted)
                {
                    return;
                }
                _startableState = StartableState.Stopping;
            }

            try
            {
                InternalStop();
                _startableState = StartableState.Stopped;
            }
            catch (Exception exception)
            {
                _startableState = StartableState.FailedToStop;
                throw new LogJamException("Exception stopping " + this, exception, this);
            }
        }
        /// <summary>
        /// Starts the manager whether or not <c>Start()</c> has already been called.
        /// </summary>
        /// <remarks>To avoid starting more than once, use <see cref="EnsureAutoStarted" />.</remarks>
        public void Start()
        {
            var    tracer    = SetupTracerFactory.TracerFor(this);
            string className = GetType().Name;

            if (IsDisposed)
            {
                tracer.Error(className + " cannot be started; it has been Dispose()ed.");
                return;
            }
            var state = _startableState;

            if ((state == StartableState.Starting) || (state == StartableState.Stopping))
            {
                tracer.Error(className + " cannot be started; state is: " + state);
                return;
            }

            tracer.Debug("Starting " + className + "...");

            lock (this)
            {
                try
                {
                    _startableState = StartableState.Starting;
                    InternalStart();
                    _startableState = StartableState.Started;
                    tracer.Info(className + " started.");
                }
                catch (Exception startException)
                {
                    _startException = startException;
                    tracer.Error(startException, "Start failed: Exception occurred.");
                    _startableState = StartableState.FailedToStart;
                }
            }
        }
Example #10
0
            /// <summary>
            /// ThreadProc for the background thread.  At any one time, there should be 0 or 1 background threads for each
            /// <see cref="BackgroundMultiLogWriter" />
            /// instance.
            /// </summary>
            private void BackgroundThreadProc()
            {
                _startableState = StartableState.Started;
                _tracer.Info("Started background thread.");

                SpinWait spinWait = new SpinWait();

                while (true)
                {
                    Action action;
                    if (_backgroundActionQueue.TryDequeue(out action))
                    {
                        try
                        {
                            action();
                        }
                        catch (Exception excp)
                        {
                            _tracer.Error(excp, "Exception caught in background thread.");
                        }

                        spinWait.Reset();
                    }
                    else if (spinWait.NextSpinWillYield && _startableState == StartableState.Stopping)
                    {
                        // No queued actions, and logwriter is stopped: Time to exit the background thread
                        break;
                    }
                    else
                    {
                        spinWait.SpinOnce();
                    }
                }

                _tracer.Info("Exiting background thread.");
                _thread = null;
            }
Example #11
0
 protected Startable()
 {
     _startableState = StartableState.Unstarted;
 }
Example #12
0
 public void Stop()
 {
     State = StartableState.CancelRequested;
 }