CannotUnloadAppDomainException
.SystemEvents
. When using System.Drawing.SolidBrush
, the handler is added to SystemEvents.UserPreferenceChanging
, but since this is a separate appdomain, the type is initialized again, another stream is created with the name ".NET SystemEvents", which, when started, adds its own to the console handlers.SystemEvents.Dispose()
is called, and there when the UnsafeNativeMethods.SetConsoleCtrlHandler(consoleHandler, 0)
console handler is UnsafeNativeMethods.SetConsoleCtrlHandler(consoleHandler, 0)
application hangs. I naguglil couple of mentions of a problem with removal of console handlers, but all solutions were reduced to a call of SystemEvents.Shutdown()
through Reflection, but it did not help me, and should not seem to help, it then causes Dispose ().SystemEvents.EnsureSystemEvents(bool requireHandle, bool throwOnRefusal)
method SystemEvents.EnsureSystemEvents(bool requireHandle, bool throwOnRefusal)
: if (Thread.GetDomain().GetData(".appDomain") != null) { if (throwOnRefusal) { throw new InvalidOperationException(SR.GetString(SR.ErrorSystemEventsNotSupported)); } return; } // SystemEvents
domain.SetData(".appDomain", new object())
avoids creating another SystemEvents stream and freezing when you remove its console handler, the application shuts down correctly.Source: https://habr.com/ru/post/238333/
All Articles