public ServiceViewModel(ServiceBase service) { Name = service.ServiceName; //Get an observable for the current state var currentStateObs = this.ObservableForProperty(x => x.CurrentState).Value().StartWith(ServiceState.Stopped); //Map an observable to IsBusy that is True if the current state is *ing currentStateObs.Select ( s => s == ServiceState.Pausing || s == ServiceState.Starting || s == ServiceState.Stopping ) .Subscribe(busy => IsBusy = busy); StartCommand = new ReactiveCommand(currentStateObs.Select(s => s == ServiceState.Stopped)); StopCommand = new ReactiveCommand(currentStateObs.Select(s => s == ServiceState.Started || s == ServiceState.Paused)); PauseCommand = new ReactiveCommand(currentStateObs.Select(s => s == ServiceState.Started && service.CanPauseAndContinue)); ContinueCommand = new ReactiveCommand(currentStateObs.Select(s => s == ServiceState.Paused && service.CanPauseAndContinue)); AssignmentSubscription(StartCommand, () => ServiceBaseHelpers.StartService(service)); AssignmentSubscription(StopCommand, () => ServiceBaseHelpers.StopService(service)); AssignmentSubscription(PauseCommand, () => ServiceBaseHelpers.PauseService(service)); AssignmentSubscription(ContinueCommand, () => ServiceBaseHelpers.ContinueService(service)); }
/// <summary> /// Loads the services. /// </summary> /// <param name="services">The services.</param> /// <param name="showGuiWhenDebuggerAttached">if set to <c>true</c> shows the GUI when debugger is attached.</param> /// <param name="showGuiWhenArgumentDetected"> /// if set to <c>true</c> shows GUI when argument detected (/startService) by /// default. /// </param> /// <param name="argumentToDetect">The argument to detect. (/startService) by default</param> /// <param name="startServiceImmediatelyWhenDebuggerAttached"> /// if set to <c>true</c> [start service immediately when /// debugger attached] - Auto start on debugger /// </param> /// <param name="startServiceImmediatelyWhenArgumentDetected"> /// if set to <c>true</c> [start service immediately when /// argument detected] - Auto start on cli param /// </param> public static void LoadServices(this IEnumerable <ServiceBase> services, bool showGuiWhenDebuggerAttached = true, bool showGuiWhenArgumentDetected = false, string argumentToDetect = "/startService", bool startServiceImmediatelyWhenDebuggerAttached = true, bool startServiceImmediatelyWhenArgumentDetected = true) { bool startServiceImmediately = ShouldAutoStart( startServiceImmediatelyWhenDebuggerAttached, startServiceImmediatelyWhenArgumentDetected, argumentToDetect); List <ServiceBase> servicesAll = services.ToList(); // OPTION 1 - Run via the GUI if (ShouldShowGui(showGuiWhenDebuggerAttached, showGuiWhenArgumentDetected, argumentToDetect)) { ShowGUI(servicesAll, startServiceImmediately); } // OPTION 2 - Run as an app WITHOUT the gui, but also not as a full windows service else if (startServiceImmediately) { Task t = Task.Factory.StartNew( () => { int servicesRunning = 0; foreach (ServiceBase x in servicesAll) { ServiceBaseHelpers.StartService(x); servicesRunning++; } try { WaitForAllServicesToExit(servicesAll, servicesRunning); } catch (Exception e) // above is a bit dodgy, so if it fails, just wait forever { while (true) { Thread.Sleep(100); } } }, CancellationToken.None, TaskCreationOptions.PreferFairness, new StaTaskScheduler(25) ); t.Wait(); } else { // OPTION 3 - Run as a full windows service ServiceBase.Run(servicesAll.ToArray()); } }