/// <summary> /// Process server commands. Returns TRUE if the application should exit. /// </summary> async Task <bool> ProcessServerCommands() { if (Program.ParamControlPipe == "") { return(false); } try { Log.Write("Starting pipe..."); mProcessManager = new ProcessManager(Program.ParamControlPipe, false); Log.Write("Pipe connected"); await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CONNECTED); while (!mExiting) { var command = await mProcessManager.ReadCommandAsync(); if (command.StartsWith(ProcessManager.COMMAND_CLOSE)) { await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CLOSING + "@reason=Closed by service"); return(true); } } } catch (Exception ex) { Log.Write("ProcessServerCommands", ex); } return(false); }
async Task ProcessServerCommands() { try { mProcessManager = new ProcessManager(Program.ParamControlPipe, false); await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CONNECTED); var commandTask = mProcessManager.ReadCommandAsync(); // Poll for desktop change every POLL_FOR_DESKTOP_CHANGE_MS milliseconds, // and close application when COMMAND_CLOSE is received while (true) { var desktop = Wts.GetDesktopName(); if (desktop != mDesktop) { await mProcessManager.SendCommandAsync(desktop); } mDesktop = desktop; if (commandTask.IsCompleted) { var command = commandTask.Result; commandTask = mProcessManager.ReadCommandAsync(); if (command.StartsWith(ProcessManager.COMMAND_CLOSE)) { await mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CLOSING + "@reason=Closed by service"); break; } } commandTask.Wait(POLL_FOR_DESKTOP_CHANGE_MS); } } catch (Exception ex) { try { mProcessManager.SendCommandAsync(ProcessManager.COMMAND_CLOSING + "@reason=Exception: " + ex.Message + ", Stack: " + ex.StackTrace.Replace("\n", "").Replace("\r", "")).Wait(100); } catch { } } }
/// <summary> /// Monitor GUI and DESKTOP processes /// </summary> async void ProcessRemoteCommunications(ProcessManager manager, string processName) { try { await manager.WaitForConnection(); var command = ""; while (!command.StartsWith(ProcessManager.COMMAND_CLOSING)) { command = await manager.ReadCommandAsync(); if (command.StartsWith(ProcessManager.COMMAND_CONNECTED)) { Log.Write(processName + " process connected on pipe " + manager.PipeName); } else if (!command.StartsWith("$")) { // For now, all non-control commands are just the desktop name if (command != mDesktopFromWatcher) { Log.Write("Desktop changed to: " + command); mDesktopFromWatcher = command; CheckServiceRunning(); // Restart GUI on new desktop } } } Log.Write(processName + " process closed itself on pipe " + manager.PipeName + ": " + command); lock (mServiceLock) { if (manager == mDesktopMan) { mDesktopFromWatcher = ""; // Prevent launching new GUI until we get a valid desktop } } } catch (Exception ex) { Log.Write(processName + " process exception: Pipe=" + manager.PipeName + ", Exception=" + ex.GetType() + ", Message=" + ex.Message); } CheckServiceRunning(); }