private void SetPid() { var process = ServiceControl.ServiceToProcess(Service); if (process != null && process.Id != 0) { _pid = process.Id; } else { _pid = null; } }
// Avoid calling on UI thread. public ServiceItem(ServiceController service, IVisibilitySettings settings) { _settings = settings; Service = service; DisplayName = service.DisplayName; ServiceName = service.ServiceName; Status = service.Status.ToString(); AttachName = "Attach";// TODO: attached or not, TODO part of the command? var process = ServiceControl.ServiceToProcess(Service); AttachCommand = new ActionCommand(() => { }, Service.Status == ServiceControllerStatus.Running && VsEnvironment.Debuggers.Any()); var debugger = VsEnvironment.Debuggers.FirstOrDefault(p => p.IsAttachedTo(process.Id)); DetachCommand = new ActionCommand(() => { Detach(debugger); }, Service.Status == ServiceControllerStatus.Running && VsEnvironment.Debuggers.Any()); StartName = Service.Status == ServiceControllerStatus.Running ? "Stop" : "Start"; // TODO: intermediate states StartServiceCommand = new ActionCommand(Start); StopServiceCommand = new ActionCommand(Stop); SetPid(); if (Service.Status == ServiceControllerStatus.Running) { StartVisibility = System.Windows.Visibility.Collapsed; StopVisibility = System.Windows.Visibility.Visible; AttachVisibility = (debugger == null) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; DetachVisibility = (debugger == null) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible; } else { StartVisibility = System.Windows.Visibility.Visible; StopVisibility = System.Windows.Visibility.Collapsed; AttachVisibility = System.Windows.Visibility.Visible; DetachVisibility = System.Windows.Visibility.Collapsed; } }