@@ -110,15 +110,26 @@ internal ProcessService(
110110 public async Task < ObservableCollection < ProcessModel > > GetProcessesAsync ( )
111111 {
112112 return await Task . Run ( ( ) =>
113+ new ObservableCollection < ProcessModel > ( this . EnumerateProcessModels ( ) ) ) . ConfigureAwait ( false ) ;
114+ }
115+
116+ private List < ProcessModel > EnumerateProcessModels ( Func < ProcessModel , bool > ? filter = null )
117+ {
118+ var foregroundProcessId = this . foregroundProcessService ? . TryGetForegroundProcessId ( ) ;
119+ var models = new List < ProcessModel > ( ) ;
120+ foreach ( var process in Process . GetProcesses ( ) )
113121 {
114- var foregroundProcessId = this . foregroundProcessService ? . TryGetForegroundProcessId ( ) ;
115- var processes = Process . GetProcesses ( )
116- . Select ( process => this . TryCreateProcessModel ( process , foregroundProcessId ) )
117- . OfType < ProcessModel > ( )
118- . OrderBy ( p => p . Name ) ;
122+ using ( process )
123+ {
124+ var model = this . TryCreateProcessModel ( process , foregroundProcessId ) ;
125+ if ( model != null && ( filter == null || filter ( model ) ) )
126+ {
127+ models . Add ( model ) ;
128+ }
129+ }
130+ }
119131
120- return new ObservableCollection < ProcessModel > ( processes ) ;
121- } ) . ConfigureAwait ( false ) ;
132+ return models . OrderBy ( process => process . Name ) . ToList ( ) ;
122133 }
123134
124135 private sealed class CpuSample
@@ -988,7 +999,7 @@ public async Task<bool> ClearProcessCpuSetAsync(ProcessModel process)
988999 {
9891000 try
9901001 {
991- var process = Process . GetProcessById ( processId ) ;
1002+ using var process = Process . GetProcessById ( processId ) ;
9921003 return this . CreateProcessModel ( process ) ;
9931004 }
9941005 catch
@@ -1000,20 +1011,29 @@ public async Task<bool> ClearProcessCpuSetAsync(ProcessModel process)
10001011
10011012 public async Task < IEnumerable < ProcessModel > > GetProcessesByNameAsync ( string executableName )
10021013 {
1003- return await Task . Run ( ( ) =>
1014+ return await Task . Run < IEnumerable < ProcessModel > > ( ( ) =>
10041015 {
10051016 try
10061017 {
10071018 var foregroundProcessId = this . foregroundProcessService ? . TryGetForegroundProcessId ( ) ;
1008- var processes = Process . GetProcessesByName ( executableName )
1009- . Select ( process => this . TryCreateProcessModel ( process , foregroundProcessId ) )
1010- . OfType < ProcessModel > ( ) ;
1019+ var models = new List < ProcessModel > ( ) ;
1020+ foreach ( var process in Process . GetProcessesByName ( executableName ) )
1021+ {
1022+ using ( process )
1023+ {
1024+ var model = this . TryCreateProcessModel ( process , foregroundProcessId ) ;
1025+ if ( model != null )
1026+ {
1027+ models . Add ( model ) ;
1028+ }
1029+ }
1030+ }
10111031
1012- return processes ;
1032+ return models ;
10131033 }
10141034 catch
10151035 {
1016- return Enumerable . Empty < ProcessModel > ( ) ;
1036+ return Array . Empty < ProcessModel > ( ) ;
10171037 }
10181038 } ) . ConfigureAwait ( false ) ;
10191039 }
@@ -1026,32 +1046,15 @@ public async Task<bool> IsProcessRunningAsync(string executableName)
10261046
10271047 public async Task < IEnumerable < ProcessModel > > GetProcessesWithPathsAsync ( )
10281048 {
1029- return await Task . Run ( ( ) =>
1030- {
1031- var foregroundProcessId = this . foregroundProcessService ? . TryGetForegroundProcessId ( ) ;
1032- var processes = Process . GetProcesses ( )
1033- . Select ( process => this . TryCreateProcessModel ( process , foregroundProcessId ) )
1034- . OfType < ProcessModel > ( )
1035- . Where ( p => ! string . IsNullOrEmpty ( p . ExecutablePath ) )
1036- . OrderBy ( p => p . Name ) ;
1037-
1038- return processes ;
1039- } ) . ConfigureAwait ( false ) ;
1049+ return await Task . Run < IEnumerable < ProcessModel > > ( ( ) =>
1050+ this . EnumerateProcessModels ( process => ! string . IsNullOrEmpty ( process . ExecutablePath ) ) ) . ConfigureAwait ( false ) ;
10401051 }
10411052
10421053 public async Task < ObservableCollection < ProcessModel > > GetActiveApplicationsAsync ( )
10431054 {
10441055 return await Task . Run ( ( ) =>
1045- {
1046- var foregroundProcessId = this . foregroundProcessService ? . TryGetForegroundProcessId ( ) ;
1047- var processes = Process . GetProcesses ( )
1048- . Select ( process => this . TryCreateProcessModel ( process , foregroundProcessId ) )
1049- . OfType < ProcessModel > ( )
1050- . Where ( p => p . HasVisibleWindow )
1051- . OrderBy ( p => p . Name ) ;
1052-
1053- return new ObservableCollection < ProcessModel > ( processes ) ;
1054- } ) . ConfigureAwait ( false ) ;
1056+ new ObservableCollection < ProcessModel > (
1057+ this . EnumerateProcessModels ( process => process . HasVisibleWindow ) ) ) . ConfigureAwait ( false ) ;
10551058 }
10561059
10571060 public async Task < bool > IsProcessStillRunning ( ProcessModel process )
@@ -1060,8 +1063,8 @@ public async Task<bool> IsProcessStillRunning(ProcessModel process)
10601063 {
10611064 try
10621065 {
1063- var p = Process . GetProcessById ( process . ProcessId ) ;
1064- return ! p . HasExited ;
1066+ using var targetProcess = Process . GetProcessById ( process . ProcessId ) ;
1067+ return ! targetProcess . HasExited ;
10651068 }
10661069 catch ( ArgumentException )
10671070 {
0 commit comments