Exemple #1
0
 private void btnCount_Click(object sender, EventArgs e)
 {
     Console.WriteLine();
     Console.WriteLine($"Exceptions count: {ExceptionCounter.Calculate(_InstanceName)}");
 }
Exemple #2
0
        public virtual async Task Start()
        {
            try
            {
                await this.Stop();
            }
            catch (Exception ex)
            {
                await Utility.CreateLogger(nameof(NanoServiceBase), nameof(Start)).Code(911).Error(ex).SaveAsync();
            }



            CancellationToken token = this._cancellation.Token;

            //the Execute method will be called below so if a derived type does not use the cancellationToken then this will be the default behaviour.
            try
            {
                token.Register(() =>
                {
                    this.IsRunning = false;
                    this.Notify("The service is cancelling.");
                    this.OnStateChangedSafely(ServiceState.Cancelling);
                });
            }
            catch (Exception)
            {
                this.IsRunning = false;
                this.Notify("An exception occurred while cancelling.");
                this.OnStateChangedSafely(ServiceState.ErrorOccuredWhenCancelling);
            }


            this.Notify("The service is startting.");
            this.OnStateChangedSafely(ServiceState.Starting);

            this.IsRunning = true;
            this.ExecutedOperationCount = -1;
            ExceptionCounter exceptionCounter = new ExceptionCounter(this.MaxErrorLimit < 1 ? int.MaxValue : this.MaxErrorLimit);//Burası Konfigure edilebilir.
            ITaskRunner      runner           = this.CreateTaskRunner();

            while (this.Continue())
            {
                try
                {
                    await runner.Run(this.Execute, this.Observer, token);
                }
                catch (Exception ex)
                {
                    await Utility.CreateLogger(nameof(NanoServiceBase), nameof(Start)).Code(911).Error(ex).SaveAsync();

                    if (exceptionCounter.CheckIfMaxExceptionOccur(ex))
                    {
                        this.IsRunning = false;
                        this.OnStateChangedSafely(ServiceState.FailedDueToMaxErrorExceed);
                        string msg = $"{this.GetType()} has been stopped due to {(this.MaxErrorLimit < 2 ? "an exception" : "a repeated exception.")}";
                        this.Notify(msg);
                        await Utility.CreateLogger(nameof(NanoServiceBase), nameof(Start)).Code(913).Warning(msg).SaveAsync();
                    }
                }

                await Wait(this.Interval.TotalMilliseconds.ConvertTo <int>());
            }

            this.IsRunning = false;                          //her iştimale karşı. örneğin CompletedDueToMaxOperationLimit' de.

            if (this._currentState == ServiceState.Starting) //yani herhangi bir nedenden durmamış. örneğin once seçilip çalıştırılıp tamamlanmış.
            {
                this.Notify("The service has been completed due to max operation limit.");
                this.OnStateChangedSafely(ServiceState.CompletedDueToMaxOperationLimit);
            }
            //else//Stop dan dolayı yapılmışsa burası zaten çalışıyor. CompletedDueToMaxOperationLimit ve FailedDueToMaxErrorExceed ise zastop edilmeden durmuş oluyor servis zaten.
            //{
            //    this.Notify("The Service has been stopped.");
            //    this.OnStateChangedSafely(ServiceState.Stopped);
            //}
        }