public async Task <bool> CheckAndOpenWindow <T>(Application app, ISingleObject <T> obj) where T : Window, new() { if (ExistAnotherInstance) { await SendOpenWindowMessage(); return(true); } else { RegistClient <T>(app, obj); return(false); } }
private void RegistClient <T>(Application app, ISingleObject <T> obj) where T : Window, new() { SimplePipe.Clinet pipe = new SimplePipe.Clinet(Name + "_Mutex"); pipe.Start(); pipe.GotMessage += (p1, p2) => { if (p2.Message == "OpenWindow") { app.Dispatcher.Invoke(() => { if (obj.SingleObject == null) { obj.SingleObject = new T(); } try { obj.SingleObject.Show(); } catch (InvalidOperationException) { obj.SingleObject = new T(); obj.SingleObject.Show(); } if (obj.SingleObject.Visibility == Visibility.Hidden) { obj.SingleObject.Visibility = Visibility.Collapsed; } if (obj.SingleObject.Visibility != Visibility.Visible) { obj.SingleObject.Visibility = Visibility.Visible; } if (obj.SingleObject.WindowState == WindowState.Minimized) { obj.SingleObject.WindowState = WindowState.Normal; } obj.SingleObject.Activate(); //pipe.Dispose(); //pipe.Start(); //SetForegroundWindow(new WindowInteropHelper(app.MainWindow).Handle); }); } }; }