File tree 2 files changed +28
-5
lines changed
src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices
2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change 10
10
using System . ServiceProcess ;
11
11
using Microsoft . CmdPal . Ext . WindowsServices . Commands ;
12
12
using Microsoft . CmdPal . Ext . WindowsServices . Properties ;
13
- using Microsoft . CommandPalette . Extensions ;
14
13
using Microsoft . CommandPalette . Extensions . Toolkit ;
15
14
using Microsoft . Win32 ;
16
15
using Windows . System ;
@@ -44,9 +43,14 @@ public static IEnumerable<ListItem> Search(string search)
44
43
serviceList = servicesStartsWith . Concat ( servicesContains ) ;
45
44
}
46
45
47
- return serviceList . Select ( s =>
46
+ var result = serviceList . Select ( s =>
48
47
{
49
- var serviceResult = new ServiceResult ( s ) ;
48
+ var serviceResult = ServiceResult . CreateServiceController ( s ) ;
49
+ if ( serviceResult == null )
50
+ {
51
+ return null ;
52
+ }
53
+
50
54
ServiceCommand serviceCommand ;
51
55
CommandContextItem [ ] moreCommands ;
52
56
if ( serviceResult . IsRunning )
@@ -93,7 +97,9 @@ public static IEnumerable<ListItem> Search(string search)
93
97
// ToolTipData = new ToolTipData(serviceResult.DisplayName, serviceResult.ServiceName),
94
98
// IcoPath = icoPath,
95
99
} ;
96
- } ) ;
100
+ } ) . Where ( s => s != null ) ;
101
+
102
+ return result ;
97
103
}
98
104
99
105
// TODO GH #118 IPublicAPI contextAPI isn't used anymore, but we need equivalent ways to show notifications and status
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ public class ServiceResult
17
17
18
18
public bool IsRunning { get ; }
19
19
20
- public ServiceResult ( ServiceController serviceController )
20
+ private ServiceResult ( ServiceController serviceController )
21
21
{
22
22
ArgumentNullException . ThrowIfNull ( serviceController ) ;
23
23
@@ -26,4 +26,21 @@ public ServiceResult(ServiceController serviceController)
26
26
StartMode = serviceController . StartType ;
27
27
IsRunning = serviceController . Status != ServiceControllerStatus . Stopped && serviceController . Status != ServiceControllerStatus . StopPending ;
28
28
}
29
+
30
+ public static ServiceResult CreateServiceController ( ServiceController serviceController )
31
+ {
32
+ try
33
+ {
34
+ var result = new ServiceResult ( serviceController ) ;
35
+
36
+ return result ;
37
+ }
38
+ catch ( Exception )
39
+ {
40
+ // try to log the exception in the future
41
+ // retrieve properties from serviceController will throw exception. Such as PlatformNotSupportedException.
42
+ }
43
+
44
+ return null ;
45
+ }
29
46
}
You can’t perform that action at this time.
0 commit comments