private static void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (!(e.ExceptionObject is Exception exception))
            {
                // while in C# what you throw must be an Exception, the CLR does not require this (see: https://stackoverflow.com/questions/913472/why-is-unhandledexceptioneventargs-exceptionobject-an-object-and-not-an-exception)
                exception = new Exception($"Exception object is not an Exception! ({e.ExceptionObject?.ToString()})");
            }

            if (e.IsTerminating)
            {
                // save critical work first, hopefully handle regular events afterwards
                MainEventQueue.EventHandling.Suspend();
                try
                {
                    MainEventQueue.HandleCritical(new CriticalUnhandledExceptionEvent(exception));
                    MoveToState(AppState.Shutdown);
                }
                finally
                {
                    MainEventQueue.EventHandling.Resume();
                }
            }
            else
            {
                EnqueueException(exception);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the (Mechanical4) app.
        /// </summary>
        /// <param name="eventQueue">The <see cref="EventQueueBase"/> to wrap.</param>
        protected static void Initialize(EventQueueBase eventQueue)
        {
            var wrapper = new MainEventQueue(eventQueue); // tests the argument implicitly

            isInitialized.SetIfEquals(newValue: true, comparand: false, out bool oldValue);
            if (oldValue)
            {
                throw new InvalidOperationException("The (Mechanical4) app may only be initialized once!");
            }

            mainEventQueue = wrapper;
            InitializeUnhandledExceptionCatching();
        }
 internal Regular(MainEventQueue mainQueue)
 {
     this.mainEventQueue = mainQueue ?? throw Exc.Null(nameof(mainQueue));
 }