private static void RunResultPipServer(Application app) { while (true) { try { NamedPipeServerStream pipeServer = new NamedPipeServerStream("resultPipName", PipeDirection.InOut, 2); pipeServer.WaitForConnection(); StreamReader sr = new StreamReader(pipeServer); string recData = sr.ReadLine(); if (recData == "MainWindowShowed") { Execute.OnUIThread(() => { app.Shutdown(); }); } sr.Close(); } catch (Exception ex) { Global.Logger.Error(ex.Message, ex); } } }
public static void ShowMainWindow(Application app, string appPipName) { Task.Factory.StartNew(() => { RunResultPipServer(app); }); try { var pipeClient = new NamedPipeClientStream(".", appPipName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation); pipeClient.Connect(200); StreamWriter sw = new StreamWriter(pipeClient); sw.WriteLine("ShowMainWindow"); sw.Flush(); Thread.Sleep(1000); sw.Close(); } catch (Exception ex) { NTMinerClientDaemon.Instance.RestartNTMiner(NTMinerRoot.MinerClientHost, 3336, CommandLineArgs.WorkId, null); Global.Logger.Error(ex.Message, ex); } TimeSpan.FromSeconds(2).Delay().ContinueWith((t) => { Process thisProcess = Process.GetCurrentProcess(); foreach (var process in Process.GetProcessesByName(thisProcess.ProcessName)) { if (process.Id != thisProcess.Id) { try { Windows.TaskKill.Kill(process.Id); } catch { } } } Windows.Cmd.RunClose(ClientId.AppFileFullName, string.Join(" ", CommandLineArgs.Args)); Execute.OnUIThread(() => { app.Shutdown(); }); }); }
protected override void OnStartup(StartupEventArgs e) { BootLog.Log("App.OnStartup start"); RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly; try { appMutex = new Mutex(true, _appPipName, out createdNew); } catch (Exception) { createdNew = false; } if (createdNew) { if (!NTMiner.Windows.Role.IsAdministrator) { AppHelper.RunAsAdministrator(); return; } BootLog.Log("new SplashWindow"); SplashWindow splashWindow = new SplashWindow(); splashWindow.Show(); NTMinerRoot.Inited = () => { NTMinerRoot.KernelDownloader = new KernelDownloader(); Execute.OnUIThread(() => { bool?result = true; if (string.IsNullOrEmpty(Server.LoginName) || string.IsNullOrEmpty(Server.Password)) { LoginWindow loginWindow = new LoginWindow(); splashWindow.Hide(); result = loginWindow.ShowDialog(); } if (result.HasValue && result.Value) { BootLog.Log("new MainWindow"); ControlCenterWindow window = new ControlCenterWindow(); IMainWindow mainWindow = window; this.MainWindow = window; this.MainWindow.Show(); this.MainWindow.Activate(); BootLog.Log("MainWindow showed"); notifyIcon = new ExtendedNotifyIcon("pack://application:,,,/ControlCenterApp;component/logo.ico"); notifyIcon.Init(); #region 处理显示主界面命令 Global.Access <ShowMainWindowCommand>( Guid.Parse("01f3c467-f494-42b8-bcb5-848050df59f3"), "处理显示主界面命令", LogEnum.None, action: message => { Execute.OnUIThread(() => { Dispatcher.Invoke((ThreadStart)mainWindow.ShowThisWindow); AppHelper.MainWindowShowed(); }); }); #endregion Task.Factory.StartNew(() => { AppHelper.RunPipeServer(this, _appPipName); }); } splashWindow?.Close(); BootLog.SyncToDisk(); }); }; } else { try { AppHelper.ShowMainWindow(this, _appPipName); } catch (Exception) { DialogWindow.ShowDialog(message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。", title: "alert", icon: "Icon_Error"); Process currentProcess = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName); foreach (var process in processes) { if (process.Id != currentProcess.Id) { NTMiner.Windows.TaskKill.Kill(process.Id); } } } } base.OnStartup(e); BootLog.Log("App.OnStartup end"); BootLog.SyncToDisk(); }
private void OnNTMinerRootInited() { OhGodAnETHlargementPill.OhGodAnETHlargementPillUtil.Access(); NTMinerRoot.KernelDownloader = new KernelDownloader(); Execute.OnUIThread(() => { Global.Logger.InfoDebugLine("new MainWindow"); Window splashWindow = MainWindow; MainWindow window = new MainWindow(); IMainWindow mainWindow = window; this.MainWindow = window; this.MainWindow.Show(); this.MainWindow.Activate(); Global.Logger.InfoDebugLine("MainWindow showed"); notifyIcon = new ExtendedNotifyIcon("pack://application:,,,/NTMiner;component/logo.ico"); notifyIcon.Init(); #region 处理显示主界面命令 Global.Access <ShowMainWindowCommand>( Guid.Parse("01f3c467-f494-42b8-bcb5-848050df59f3"), "处理显示主界面命令", LogEnum.None, action: message => { Execute.OnUIThread(() => { Dispatcher.Invoke((ThreadStart)mainWindow.ShowThisWindow); }); }); #endregion #region 处理重启NTMiner命令 Global.Access <RestartNTMinerCommand>( Guid.Parse("d1712c1f-507c-496f-9da2-870cbd9fc57f"), "处理重启NTMiner命令", LogEnum.None, action: message => { List <string> args = CommandLineArgs.Args; if (message.IsWorkEdit) { if (CommandLineArgs.IsWorkEdit && CommandLineArgs.WorkId == message.MineWorkId) { Execute.OnUIThread(() => { Dispatcher.Invoke((ThreadStart)mainWindow.ShowThisWindow); }); return; } if (!CommandLineArgs.IsControlCenter) { args.Add("--controlCenter"); } } if (message.MineWorkId != Guid.Empty) { if (!CommandLineArgs.IsWorker) { args.Add("--workid=" + message.MineWorkId.ToString()); } else { for (int i = 0; i < args.Count; i++) { if (args[i].StartsWith("--workid=", StringComparison.OrdinalIgnoreCase)) { args[i] = "--workid=" + message.MineWorkId.ToString(); break; } } } } else { if (CommandLineArgs.IsWorker) { int workIdIndex = -1; for (int i = 0; i < args.Count; i++) { if (args[i].ToLower().Contains("--workid=")) { workIdIndex = i; break; } } if (workIdIndex != -1) { args.RemoveAt(workIdIndex); } } } NTMiner.Windows.Cmd.RunClose(ClientId.AppFileFullName, string.Join(" ", args)); Current.MainWindow.Close(); }); #endregion try { NTMinerRoot.Current.Start(); } catch (Exception ex) { Global.Logger.ErrorDebugLine(ex.Message, ex); } splashWindow?.Close(); if (NTMinerRoot.Current.MinerProfile.IsAutoStart || CommandLineArgs.IsAutoStart) { Global.Logger.InfoDebugLine("自动开始挖矿倒计时"); Views.Ucs.AutoStartCountdown.ShowDialog(); } }); }