/// <summary> /// Hooks the console using the <see cref="NativeMethods"/> class methods that are exported from the windows api dlls in order to /// allow the console to show even when the project output type is set to Windows Application, that way we can swap between <see cref="Gui"/> /// or <see cref="Tui"/>. /// Also registers a callback method with the <see cref="NativeMethods.SetConsoleCtrlHandler"/> that will register a /// close event to the console even if someones click the X button on the top right side so we can do last wrapping before /// console closes. /// </summary> private static void HookConsole() { _startGui = false; ClosedHandler += CloseCallback; NativeMethods.AllocConsole(); NativeMethods.SetConsoleCtrlHandler(ClosedHandler, true); AppDomain.CurrentDomain.ProcessExit += delegate { ClosedHandler?.Invoke(CtrlType.CLOSE); }; }
private static void RaiseClosed(IntPtr cPtr, IntPtr sender, IntPtr e) { try { if (Noesis.Extend.Initialized) { long ptr = cPtr.ToInt64(); if (sender == IntPtr.Zero && e == IntPtr.Zero) { _Closed.Remove(ptr); return; } ClosedHandler handler = null; if (!_Closed.TryGetValue(ptr, out handler)) { throw new InvalidOperationException("Delegate not registered for Closed event"); } handler?.Invoke(Noesis.Extend.GetProxy(sender, false), new EventArgs(e, false)); } } catch (Exception exception) { Noesis.Error.UnhandledException(exception); } }
public void OnClosed(TResult selection) => ClosedHandler?.Invoke(selection);