static public void StartStopService(bool start) { lock (instanceContext) { try { //if(!WindowsUserRoutines.CurrentUserHasElevatedPrivileges()) if (!ProcessRoutines.ProcessHasElevatedPrivileges() && !ProcessRoutines.ProcessIsSystem() /*used for configuration during installing*/) { if (Message.YesNo("This action requires elevated privileges. Would you like to restart this application 'As Administrator'?")) { ProcessRoutines.Restart(true); } return; } double timeoutSecs = 20; using (ServiceController serviceController = new ServiceController(Cliver.CisteraScreenCaptureService.Program.SERVICE_NAME)) { if (start) { serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(timeoutSecs)); if (serviceController.Status != ServiceControllerStatus.Running) { Message.Error("Could not start service '" + Cliver.CisteraScreenCaptureService.Program.SERVICE_NAME + "' within " + timeoutSecs + " secs."); } } else { serviceController.Stop(); serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(timeoutSecs)); if (serviceController.Status != ServiceControllerStatus.Stopped) { Message.Error("Could not stop service '" + Cliver.CisteraScreenCaptureService.Program.SERVICE_NAME + "' within " + timeoutSecs + " secs."); } } } } catch (Exception ex) { LogMessage.Error(ex); } finally { } } }
SettingsWindow() { InitializeComponent(); System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this); Icon = AssemblyRoutines.GetAppIconImageSource(); ContentRendered += delegate { //this.MinHeight = this.ActualHeight; //this.MaxHeight = this.ActualHeight; //this.MinWidth = this.ActualWidth; }; WpfRoutines.AddFadeEffect(this, 300); WindowStartupLocation = WindowStartupLocation.CenterScreen; //WindowStartupLocation = WindowStartupLocation.CenterScreen; //DefaultServerIp.ValueDataType = typeof(IPAddress); general = UiApiClient.GetServiceSettings(); if (general == null) { ok.IsEnabled = false; reset.IsEnabled = false; Message.Error("The service is unavailable."); return; } set(); if (!ProcessRoutines.ProcessHasElevatedPrivileges() && !ProcessRoutines.ProcessIsSystem() /*used for configuration during installing*/) { ok.IsEnabled = false; reset.IsEnabled = false; if (Message.YesNo("Settings modification requires elevated privileges. Would you like to restart this application 'As Administrator'?")) { ProcessRoutines.Restart(true); } } }
void save(object sender, EventArgs e) { try { ushort v; //if (!ushort.TryParse(ServerDefaultPort.Text, out v)) // throw new Exception("Server port must be an integer between 0 and " + ushort.MaxValue); //general.TcpClientDefaultPort = v; if (string.IsNullOrWhiteSpace(ServerDefaultIp.Text)) { throw new Exception("Default server ip is not specified."); } IPAddress ia; if (!IPAddress.TryParse(ServerDefaultIp.Text, out ia)) { throw new Exception("Default server ip is not a valid value."); } general.TcpClientDefaultIp = ia.ToString(); if (!ushort.TryParse(ClientPort.Text, out v)) { throw new Exception("Client port must be an between 0 and " + ushort.MaxValue); } general.TcpServerPort = v; if (string.IsNullOrWhiteSpace(ServiceDomain.Text)) { throw new Exception("Service domian is not specified."); } general.ServiceDomain = ServiceDomain.Text.Trim(); if (string.IsNullOrWhiteSpace(ServiceType.Text)) { throw new Exception("Service type is not specified."); } general.ServiceType = ServiceType.Text.Trim(); if (Monitors.SelectedIndex < 0) { throw new Exception("Captured Video Source is not specified."); } general.CapturedMonitorDeviceName = (string)Monitors.SelectedValue; general.ShowMpegWindow = ShowMpegWindow.IsChecked ?? false; general.WriteMpegOutput2Log = WriteMpegOutput2Log.IsChecked ?? false; //general.CapturedMonitorRectangle = MonitorRoutines.GetMonitorAreaByMonitorName(general.CapturedMonitorDeviceName); //if (general.CapturedMonitorRectangle == null) // throw new Exception("Could not get rectangle for monitor '" + general.CapturedMonitorDeviceName + "'"); general.CapturedMonitorRectangle = null; general.DeleteLogsOlderDays = int.Parse(DeleteLogsOlderDays.Text); UiApiClient.SaveServiceSettings(general); if (Settings.View.DeleteLogsOlderDays != general.DeleteLogsOlderDays) { Settings.View.DeleteLogsOlderDays = general.DeleteLogsOlderDays; Settings.View.Save(); } System.ServiceProcess.ServiceControllerStatus?status = UiApiClient.GetServiceStatus(); if (status != null && status != System.ServiceProcess.ServiceControllerStatus.Stopped && (ProcessRoutines.ProcessIsSystem() ||/*used for configuration during installing*/ Message.YesNo("The changes have been saved and will be engaged after service restart. Would you like to restart the service (all the present connections if any, will be terminated)?") ) ) { MessageForm mf = null; ThreadRoutines.StartTry(() => { mf = new MessageForm(System.Windows.Forms.Application.ProductName, System.Drawing.SystemIcons.Information, "Resetting the service. Please wait...", null, 0, null); mf.ShowDialog(); }); UiApiClient.StartStopService(false); UiApiClient.StartStopService(true); if (null == SleepRoutines.WaitForObject(() => { return(mf); }, 1000)) { throw new Exception("Could not get MessageForm"); } mf.Invoke(() => { mf.Close(); }); } Close(); } catch (Exception ex) { Message.Exclaim(ex.Message); } }