private bool CanConnectToService(bool showErrors, out bool serviceInstalled) { serviceInstalled = false; using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name)) { try { if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.Paused) { serviceInstalled = true; return(true); } else { if (showErrors) { WinUtil.ShowError(string.Format("Cannot connect to service {0}: it is not running.", C1ReportsSchedulerService.Constants.Name)); } serviceInstalled = true; return(false); } } catch (Exception ex) { if (showErrors) { WinUtil.ShowError(string.Format("Could not access service {0}: {1}", C1ReportsSchedulerService.Constants.Name, ex.Message)); } return(false); } finally { sc.Close(); } } }
private void FileOpen() { try { if (!ConfirmNewOrLoadTasks()) { return; } using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = string.Format("{0} config files (*{1})|*{1}|All files (*.*)|*.*", C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Defaults.C1rsconfFileExt); if (ofd.ShowDialog() == DialogResult.OK) { FileOpen(ofd.FileName); } } } catch (Exception ex) { WinUtil.ShowError(string.Format("Could not load from the selected file: {0}", ex.Message)); NewTasks(); } }
private void Command_Click(object sender, EventArgs e) { // avoid recursive command calls: if (!_timer.Enabled) { return; } // we turn service pinging off for the duration of the command. _timer.Stop(); try { Command cmd = _commandMap[sender]; cmd(); } catch (Exception ex) { WinUtil.ShowError(ex.Message); } finally { _timer.Start(); } }
private void ServiceStart(ProgressDialog progress, bool unconditionalTransfer) { using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name)) { try { progress.SetProgress(5, "Checking service status..."); sc.WaitForStatus(ServiceControllerStatus.Stopped, c_timeout); ServiceControllerStatus status = sc.Status; if (status != ServiceControllerStatus.Stopped) { WinUtil.ShowError("Service is running or paused!"); return; } string currentTaskListFile = null; bool transfer = unconditionalTransfer; if (!transfer && _tasksHolder.Tasks.Count > 0) { transfer = WinUtil.AskQuestion("Transfer current task list to service?", false); } if (transfer) { progress.SetProgress(10, "Stopping running schedules..."); _tasksHolder.StopSchedules(); progress.SetProgress(20, "Preparing current task list for transfer..."); currentTaskListFile = Path.GetTempFileName(); _tasksHolder.Save(currentTaskListFile); } this.Clear(); progress.SetProgress(30, string.Format("Starting {0} service...", C1ReportsSchedulerService.Constants.Name)); if (string.IsNullOrEmpty(currentTaskListFile)) { sc.Start(); } else { sc.Start(new string[] { currentTaskListFile }); } WaitForStatus(sc, ServiceControllerStatus.Running, progress); sc.ExecuteCommand((int)C1ReportsSchedulerService.CustomCommands.ResetC1rsconfFileToRegistry); progress.SetProgress(90, "Connecting to service..."); ConnectToService(progress); progress.SetProgress(90, "Service successfully started."); if (!string.IsNullOrEmpty(currentTaskListFile)) { File.Delete(currentTaskListFile); } } catch (Exception ex) { WinUtil.ShowError(string.Format("Error: {0}", ex.Message)); } finally { sc.Close(); } } }
private void ServiceSetup() { // find out whether the service is installed or not: bool serviceInstalled = false; ServiceControllerStatus serviceStatus = ServiceControllerStatus.ContinuePending; // some assigned value using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name)) { try { serviceStatus = sc.Status; serviceInstalled = true; } catch { } finally { sc.Close(); } } // try getting values from registry, use defaults if unsuccessful: string sWcfEndpointBaseAddress = C1ReportsSchedulerService.Defaults.WcfEndpointBaseAddress; string sWcfEndpointRelAddress = C1ReportsSchedulerService.Defaults.WcfEndpointRelAddress; string sC1rsconfFilePath = string.Format(@"{0}\{1}{2}", Application.CommonAppDataPath, C1ReportsSchedulerService.Defaults.C1rsconfFileName, C1ReportsSchedulerService.Defaults.C1rsconfFileExt); bool sAutoStart = serviceInstalled ? ServiceInstaller.InstallService.GetAutoStartValue(C1ReportsSchedulerService.Constants.Name) : false; bool sLogTasks = C1ReportsSchedulerService.Defaults.LogTasks; bool sLogActions = C1ReportsSchedulerService.Defaults.LogActions; bool sLogProgramOutput = C1ReportsSchedulerService.Defaults.LogProgramOutput; bool sEnableMex = C1ReportsSchedulerService.Defaults.EnableMex; try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(C1ReportsSchedulerService.Constants.RegKey_Root)) { sWcfEndpointBaseAddress = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointBaseAddress) as string; sWcfEndpointRelAddress = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointRelAddress) as string; sC1rsconfFilePath = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_C1rsconfFilePath) as string; sLogTasks = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogTasks) != 0; sLogActions = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogActions) != 0; sLogProgramOutput = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogProgramOutput) != 0; sEnableMex = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_EnableMex) != 0; } } catch { // eat errors here } // show dialog to set up service parameters (WCF comm address etc): using (ServiceInstallDialog sid = new ServiceInstallDialog()) { sid.WcfEndpointBaseAddress = sWcfEndpointBaseAddress; sid.WcfEndpointRelAddress = sWcfEndpointRelAddress; sid.C1rsconfFilePath = sC1rsconfFilePath; sid.AutoStart = sAutoStart; sid.LogTasks = sLogTasks; sid.LogActions = sLogActions; sid.LogProgramOutput = sLogProgramOutput; sid.EnableMex = sEnableMex; if (sid.ShowDialog(this) != DialogResult.OK) { return; } // copy back values from the dialog: sWcfEndpointBaseAddress = sid.WcfEndpointBaseAddress; sWcfEndpointRelAddress = sid.WcfEndpointRelAddress; sC1rsconfFilePath = sid.C1rsconfFilePath; sAutoStart = sid.AutoStart; sLogTasks = sid.LogTasks; sLogActions = sid.LogActions; sLogProgramOutput = sid.LogProgramOutput; sEnableMex = sid.EnableMex; } // install service if it is not installed, stop (so that the new settings will be applied) otherwise: using (ProgressDialog prg = new ProgressDialog()) { if (!serviceInstalled) { // install service: prg.SetProgress(10, "Installing service..."); // find service executable (must be in same directory as current app): string serviceExe = WinUtil.FindServiceExe(); if (string.IsNullOrEmpty(serviceExe)) { WinUtil.ShowError(string.Format("Could not find the service executable {0}", WinUtil.ServiceExeName)); return; } // install service: ServiceInstaller.InstallService.Install(serviceExe, C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Constants.Name, sAutoStart); ServiceInstaller.InstallService.SetDescription(C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Constants.Description); } else { DisconnectFromService(); // stop service (restart will pick up new settings): ServiceCall(ServiceControllerStatus.Stopped, "Stopping", prg); // update "autostart" parameter: ServiceInstaller.InstallService.SetAutoStartValue(C1ReportsSchedulerService.Constants.Name, sAutoStart); } // set up service via additional registry keys: using (RegistryKey key = Registry.LocalMachine.OpenSubKey(C1ReportsSchedulerService.Constants.RegKey_Root, true)) { key.SetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointBaseAddress, sWcfEndpointBaseAddress, RegistryValueKind.String); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointRelAddress, sWcfEndpointRelAddress, RegistryValueKind.String); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_C1rsconfFilePath, sC1rsconfFilePath, RegistryValueKind.String); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogTasks, sLogTasks ? 1 : 0, RegistryValueKind.DWord); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogActions, sLogActions ? 1 : 0, RegistryValueKind.DWord); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogProgramOutput, sLogProgramOutput ? 1 : 0, RegistryValueKind.DWord); key.SetValue(C1ReportsSchedulerService.Constants.RegKey_EnableMex, sEnableMex ? 1 : 0, RegistryValueKind.DWord); } // create event log: if (!EventLog.SourceExists(C1ReportsSchedulerService.Constants.EventLogSourceName)) { // optional but why not... prg.SetProgress(prg.Complete + 5, "Creating service event log source..."); EventLog.CreateEventSource(C1ReportsSchedulerService.Constants.EventLogSourceName, C1ReportsSchedulerService.Constants.EventLogName); } // start service: ServiceStart(prg, false); } }
private void TestClientModeOnStart() { if (Properties.Settings.Default.ClientMode) { return; } bool serviceInstalled; if (CanConnectToService(false, out serviceInstalled)) { // connect to a running service: string text = "You're about to start C1ReportsScheduler app in standalone mode.\r\n" + "But, it appears that C1Reports scheduling service is installed and running.\r\n" + "For full functionality it is highly recommended that you connect to the C1Reports scheduling service, " + "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" + "Do you want to connect to the service now?\r\n" ; if (WinUtil.AskQuestion(text, false)) { Properties.Settings.Default.ClientMode = true; } } else if (serviceInstalled) { // warn about a stopped service: string text = "You're about to start C1ReportsScheduler app in standalone mode.\r\n" + "But, it appears that C1Reports scheduling service is installed but is not running.\r\n" + "For full functionality it is highly recommended that you start the C1Reports scheduling service, " + "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" + "Use menu Service | Start to start the service and connect to it.\r\n" ; WinUtil.ShowWarning(text); } else { // offer to install and start service: string text = "You're about to start C1ReportsScheduler app in standalone mode.\r\n" + "Standalone mode provides limited scheduling functionality, as scheduled reports will not run when the application is closed.\r\n" + "For full functionality it is highly recommended that you install the C1Reports scheduling service, " + "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" + "Do you want to install the service now?\r\n\r\n" + "(You can install or uninstall the service at any time via the \"Service\" menu.)" ; if (WinUtil.AskQuestion(text, false)) { try { ServiceSetup(); Properties.Settings.Default.ClientMode = true; } catch (Exception ex) { WinUtil.ShowError(ex.Message); } } } }