public CTraceLocal(JBC_API _jbc, int _FileMaxSequence = 0) { // VBConversions Note: Non-static class variable initialization is below. Class variables cannot be initially assigned non-static values in C#. m_TraceControlLogFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles), "JBC\\JBC Station Controller Service\\TraceData"); LoggerModule.InitLogger(); jbc = _jbc; FileMaxSequence = _FileMaxSequence; m_Trace = new CTraceData(6000); //CheckForIllegalCrossThreadCalls = False //butStartTrace.Enabled = True //butStopTrace.Enabled = False //nudFileMaxSequence.Enabled = butStartTrace.Enabled m_TimerStart = new System.Timers.Timer(); m_TimerStart.Elapsed += TimerStart_Elapsed; m_TimerStart.AutoReset = false; m_TimerStart.Interval = TIME_START; m_TimerStart.Stop(); m_TimerDiscover = new System.Timers.Timer(); m_TimerDiscover.Elapsed += TimerDiscover_Elapsed; m_TimerDiscover.AutoReset = false; m_TimerDiscover.Interval = TIME_DISCOVER; m_TimerDiscover.Stop(); m_TimerCopyFiles = new System.Timers.Timer(); m_TimerCopyFiles.Elapsed += TimerCopyFiles_Elapsed; m_TimerCopyFiles.AutoReset = false; m_TimerCopyFiles.Interval = TIME_COPY_FILES; m_TimerCopyFiles.Stop(); m_TimerStart.Start(); }
/// <summary> /// Install or initialize the service /// </summary> /// <returns>Return 0 is operation is succesfull, 1 otherwise</returns> public static int Main() { int retVal = 0; //Initialize the Event Log LoggerModule.InitLogger(); LoggerModule.logger.Info(Localization.getResStr(modL10nData.EV_STARTING_ID)); //It's used in install mode if (System.Environment.UserInteractive) { bool bInstall = false; bool bUninstall = false; enumAction action = enumAction.aUnknown; enumCommand search = enumCommand.seUnknown; enumCommand searchInit = enumCommand.seUnknown; int timeoutMilliseconds = 30000; List <int> listCommands = new List <int>(); // auto instala o desinstala el servicio windows foreach (string argument in (new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).CommandLineArgs) { switch (argument.ToLower()) { case "-install": action = enumAction.aInstall; break; case "-uninstall": action = enumAction.aUninstall; break; case "-stop": listCommands.Add((int)enumCommand.aStop); break; case "-start": listCommands.Add((int)enumCommand.aStart); break; case "-restart": listCommands.Add((int)enumCommand.aRestart); break; case "-initnone": listCommands.Add((int)enumCommand.initNONE); break; case "-initall": listCommands.Add((int)enumCommand.initALL); break; case "-initusb": listCommands.Add((int)enumCommand.initUSB); break; case "-initeth": listCommands.Add((int)enumCommand.initETH); break; case "-startusb": listCommands.Add((int)enumCommand.startUSB); break; case "-starteth": listCommands.Add((int)enumCommand.startETH); break; case "-stopusb": listCommands.Add((int)enumCommand.stopUSB); break; case "-stopeth": listCommands.Add((int)enumCommand.stopETH); break; } } switch (action) { case enumAction.aInstall: try { // install ManagedInstallerClass.InstallHelper(new string[] { System.Reflection.Assembly.GetExecutingAssembly().Location }); } catch (Exception ex) { LoggerModule.logger.Error("Installing service. " + ex.Message); retVal = 1; } break; case enumAction.aUninstall: try { // uninstall ManagedInstallerClass.InstallHelper(new string[] { "/u", System.Reflection.Assembly.GetExecutingAssembly().Location }); } catch (Exception ex) { LoggerModule.logger.Error("Uninstalling service. " + ex.Message); retVal = 1; } break; default: ServiceController service = new ServiceController(WINDOWS_SERVICE_NAME); if (service != null) { // it is installed TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); foreach (enumCommand comm in listCommands) { switch (comm) { case enumCommand.aStart: try { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception ex) { LoggerModule.logger.Error("Starting service. " + ex.Message); retVal = 1; } break; case enumCommand.aStop: try { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } catch (Exception ex) { LoggerModule.logger.Error("Stopping service. " + ex.Message); retVal = 1; } break; case enumCommand.aRestart: try { if (service.Status == ServiceControllerStatus.Running) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch (Exception ex) { LoggerModule.logger.Error("Restarting service. " + ex.Message); retVal = 1; } break; case enumCommand.startUSB: case enumCommand.startETH: case enumCommand.stopUSB: case enumCommand.stopETH: try { // should be started for custom commands service.WaitForStatus(ServiceControllerStatus.Running, timeout); service.ExecuteCommand((System.Int32)comm); } catch (Exception ex) { LoggerModule.logger.Error(string.Format("Error trying to custom command {0}: " + ex.Message, comm.ToString())); retVal = 1; } break; case enumCommand.initALL: case enumCommand.initETH: case enumCommand.initUSB: case enumCommand.initNONE: try { // should be started for custom commands service.WaitForStatus(ServiceControllerStatus.Running, timeout); service.ExecuteCommand((System.Int32)comm); } catch (Exception ex) { LoggerModule.logger.Error(string.Format("Error trying to custom command {0}: " + ex.Message, comm.ToString())); retVal = 1; } break; } } } //Poder ejecutar el servicio sin necesidad de instalarlo #if DEBUG_TRACECONTROLLOCAL && DEBUG My.Settings.Default.SearchUSB = true; My.Settings.Default.SearchETH = true; string[] args = new string[1]; JBCTraceControlLocalSrv serv = new JBCTraceControlLocalSrv(); serv.OnStart(args); while (true) { Console.Read(); } serv.OnStop(); #endif break; } } else { // crea el servicio windows ServiceBase.Run(new JBCTraceControlLocalSrv()); } return(retVal); }