Esempio n. 1
0
        void IModuleImplementation.ApplicationBeforeCleanup()
        {
            var msTimeout = this.ExpectedShutdownDurationMs;

            if (msTimeout < 1)
            {
                msTimeout = App.ExpectedComponentShutdownDurationMs;
            }
            if (msTimeout < 1)
            {
                msTimeout = CommonApplicationLogic.DFLT_EXPECTED_COMPONENT_SHUTDOWN_DURATION_MS;
            }

            var handled = TimedCall.Run(ct => DoApplicationBeforeCleanup(),
                                        msTimeout,
                                        () => WriteLog(MessageType.WarningExpectation,
                                                       nameof(IModuleImplementation.ApplicationBeforeCleanup),
                                                       "Module '{0}' finalization is taking longer than expected {1:n} ms".Args(Name, msTimeout))
                                        );

            if (!handled)
            {
                m_Children.OrderedValues
                .Reverse()
                .Cast <IModuleImplementation>() //explicitly def interface
                .ForEach(c => c.ApplicationBeforeCleanup());
            }
        }
Esempio n. 2
0
        public void Basic_1()
        {
            var was = false;

            TimedCall.Run(c => { /* does nothing */ }, 1000, () => was = true);
            Aver.IsFalse(was);

            TimedCall.Run(c => Thread.Sleep(750), 10, () => was = true);
            Aver.IsTrue(was);
        }
Esempio n. 3
0
        public void Cancellation_1()
        {
            var cts = new CancellationTokenSource();

            var was = false;

            TimedCall.Run(c => { /* does nothing */ }, 1000, () => was = true, cts.Token);
            Aver.IsFalse(was);

            TimedCall.Run(c => Thread.Sleep(750), 10, () => was = true, cts.Token);
            Aver.IsTrue(was);
        }
Esempio n. 4
0
        public void Basic_2()
        {
            var was = false;
            var got = TimedCall.Run(c => { /* does nothing */ return(29); }, 1000, () => was = true);

            Aver.IsFalse(was);
            Aver.AreEqual(29, got);

            got = TimedCall.Run(c => { Thread.Sleep(750); return(175); }, 10, () => was = true);
            Aver.IsTrue(was);
            Aver.AreEqual(175, got);
        }
        protected virtual void CleanupComponent <T>(ref T cmp, string name) where T : class, IDisposable
        {
            if (cmp == null)
            {
                return;
            }

            var target = cmp;

            cmp = null;

            WriteLog(MessageType.Trace, CLEANUP_FROM, "Finalizing {0}".Args(name));
            try
            {
                var msTimeout = this.ExpectedComponentShutdownDurationMs;

                if (target is IApplicationComponent ac)
                {
                    msTimeout = ac.ExpectedShutdownDurationMs;
                }

                if (msTimeout < 1)
                {
                    msTimeout = DFLT_EXPECTED_COMPONENT_SHUTDOWN_DURATION_MS;
                }

                TimedCall.Run(ct =>
                {
                    //if (target is Daemon daemon) <- needs CS 7.2 due to <T>
                    var daemon = target as Daemon;
                    if (daemon != null)
                    {
                        daemon.WaitForCompleteStop();
                        WriteLog(MessageType.Trace, CLEANUP_FROM, "{0} daemon stopped".Args(name));
                    }

                    DisposeAndNull(ref target);
                    WriteLog(MessageType.Trace, CLEANUP_FROM, "{0} disposed".Args(name));
                },
                              msTimeout,
                              () => WriteLog(MessageType.WarningExpectation, CLEANUP_FROM, "Disposal of '{0}' is taking longer than expected {1:n} ms".Args(name, msTimeout))
                              );//TimedCall
            }
            catch (Exception error)
            {
                //don't throw just log
                WriteLog(MessageType.CatastrophicError, CLEANUP_FROM, StringConsts.APP_CLEANUP_COMPONENT_ERROR.Args(name, error.ToMessageWithType()), error);
            }
        }
Esempio n. 6
0
        public void Cancellation_3()
        {
            var cts = new CancellationTokenSource();

            cts.Cancel();

            var was = false;

            TimedCall.Run(c => { for (var i = 0; i < 100 && !c.IsCancellationRequested; i++)
                                 {
                                     Thread.Sleep(25);
                                 }
                          }, 25_000, () => was = true, cts.Token);
            Aver.IsFalse(was);//it was canceled already
        }
Esempio n. 7
0
        protected override void DoWaitForCompleteStop()
        {
            base.DoWaitForCompleteStop();
            // at this point the thread has stopped and we can now stop the sinks

            var iamPrimary = object.ReferenceEquals(App.Log, this);

            foreach (var sink in m_Sinks.OrderedValues.Reverse())
            {
                try
                {
                    if (iamPrimary)
                    {
                        sink.WaitForCompleteStop();
                        continue;
                    }

                    var msTimeout = sink.ExpectedShutdownDurationMs;
                    if (msTimeout < 1)
                    {
                        msTimeout = App.ExpectedComponentShutdownDurationMs;
                    }
                    if (msTimeout < 1)
                    {
                        msTimeout = CommonApplicationLogic.DFLT_EXPECTED_COMPONENT_SHUTDOWN_DURATION_MS;
                    }
                    TimedCall.Run(_ => sink.WaitForCompleteStop(),
                                  msTimeout,
                                  () => WriteLog(MessageType.WarningExpectation,
                                                 "{0}.{1}".Args(nameof(DoWaitForCompleteStop), sink.Name),
                                                 "Awaiting sink '{0}' is taking longer than expected {1:n} ms".Args(sink.Name, msTimeout)));
                }
                catch
                {
#warning REVISE - must not eat exceptions - use Conout? use new .core ETW
                } // Can't do much here in case of an error
            }

            m_InstrBuffer.WaitForCompleteStop();
        }
Esempio n. 8
0
        private static void TimedCall(string p_name, TimedCall p_call, int p_amount = 1)
        {
            // source: http://www.bradoncode.com/blog/2012/02/benchmarking-methods-in-c-made-easier.html

            // clean up
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            System.GC.Collect();
            //warm up
            p_call();

            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < p_amount; ++i)
            {
                p_call.Invoke();
            }

            stopwatch.Stop();
            Console.WriteLine("operation {1} executed: {2} times in: {0}", stopwatch.Elapsed, p_name, p_amount);
            Console.WriteLine("avarage execution time: {0} \n", stopwatch.Elapsed / p_amount);
        }