@@ -24,7 +24,14 @@ public class ProcessWatcher
24
24
public ApplicationItem CurrentFocusedApplicationItem { get ; private set ; }
25
25
26
26
Dictionary < ApplicationItem , ApplicationState > _applications = new Dictionary < ApplicationItem , ApplicationState > ( ) ;
27
- public IReadOnlyDictionary < ApplicationItem , ApplicationState > Applications => new ReadOnlyDictionary < ApplicationItem , ApplicationState > ( _applications ) ;
27
+ public IReadOnlyDictionary < ApplicationItem , ApplicationState > Applications
28
+ {
29
+ get
30
+ {
31
+ lock ( _applicationsLock )
32
+ return new ReadOnlyDictionary < ApplicationItem , ApplicationState > ( _applications ) ;
33
+ }
34
+ }
28
35
29
36
bool _stopRequested = false ;
30
37
bool _isRunning = false ;
@@ -34,17 +41,17 @@ public class ProcessWatcher
34
41
public event EventHandler OneProcessIsRunningChanged ;
35
42
public event EventHandler OneProcessIsFocusedChanged ;
36
43
37
- ManagementEventWatcher startWatch ;
38
- ManagementEventWatcher stopWatch ;
44
+ // ManagementEventWatcher startWatch;
45
+ // ManagementEventWatcher stopWatch;
39
46
40
47
public ProcessWatcher ( )
41
48
{
42
- startWatch = new ManagementEventWatcher (
43
- new WqlEventQuery ( "SELECT * FROM Win32_ProcessStartTrace" ) ) ;
44
- stopWatch = new ManagementEventWatcher (
45
- new WqlEventQuery ( "SELECT * FROM Win32_ProcessStopTrace" ) ) ;
46
- startWatch . EventArrived += StartWatch_EventArrived ;
47
- stopWatch . EventArrived += StopWatch_EventArrived ;
49
+ // startWatch = new ManagementEventWatcher(
50
+ // new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
51
+ // stopWatch = new ManagementEventWatcher(
52
+ // new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
53
+ // startWatch.EventArrived += StartWatch_EventArrived;
54
+ // stopWatch.EventArrived += StopWatch_EventArrived;
48
55
49
56
}
50
57
@@ -55,37 +62,37 @@ private void CallNewLog(string logMessage)
55
62
}
56
63
57
64
58
- private void StartWatch_EventArrived ( object sender , EventArrivedEventArgs e )
59
- {
60
- lock ( _applicationsLock )
61
- {
62
- string applicationName = e . NewEvent . Properties [ "ProcessName" ] . Value . ToString ( ) . Replace ( ".exe" , "" ) . ToUpperInvariant ( ) ;
63
- if ( _applications . Any ( a => a . Key . ApplicationName . ToUpperInvariant ( ) . Equals ( applicationName ) ) )
64
- {
65
- ApplicationItem application = _applications . First ( a => a . Key . ApplicationName . ToUpperInvariant ( ) . Equals ( applicationName ) ) . Key ;
66
- if ( _applications [ application ] == ApplicationState . None )
67
- _applications [ application ] = ApplicationState . Running ;
68
- CallNewLog ( $ "Application startet: { application } ") ;
69
- }
70
- }
71
- }
72
-
73
- private void StopWatch_EventArrived ( object sender , EventArrivedEventArgs e )
74
- {
75
-
76
- lock ( _applicationsLock )
77
- {
78
- string applicationName = e . NewEvent . Properties [ "ProcessName" ] . Value . ToString ( ) . Replace ( ".exe" , "" ) . ToUpperInvariant ( ) ;
79
- if ( _applications . Any ( a => a . Key . ApplicationName . ToUpperInvariant ( ) . Equals ( applicationName ) ) )
80
- {
81
- ApplicationItem application = _applications . First ( a => a . Key . ApplicationName . ToUpperInvariant ( ) . Equals ( applicationName ) ) . Key ;
82
- _applications [ application ] = ApplicationState . None ;
83
- CallNewLog ( $ "Application stopped: { application } ") ;
84
- }
85
- }
86
- }
87
-
88
- public void AddProcess ( ApplicationItem application , bool updateRunningProcesses = true )
65
+ // private void StartWatch_EventArrived(object sender, EventArrivedEventArgs e)
66
+ // {
67
+ // lock (_applicationsLock)
68
+ // {
69
+ // string applicationName = e.NewEvent.Properties["ProcessName"].Value.ToString().Replace(".exe", "").ToUpperInvariant();
70
+ // if (_applications.Any(a => a.Key.ApplicationName.ToUpperInvariant().Equals(applicationName)))
71
+ // {
72
+ // ApplicationItem application = _applications.First(a => a.Key.ApplicationName.ToUpperInvariant().Equals(applicationName)).Key;
73
+ // if (_applications[application] == ApplicationState.None)
74
+ // _applications[application] = ApplicationState.Running;
75
+ // CallNewLog($"Application startet: {application}");
76
+ // }
77
+ // }
78
+ // }
79
+
80
+ // private void StopWatch_EventArrived(object sender, EventArrivedEventArgs e)
81
+ // {
82
+
83
+ // lock (_applicationsLock)
84
+ // {
85
+ // string applicationName = e.NewEvent.Properties["ProcessName"].Value.ToString().Replace(".exe", "").ToUpperInvariant();
86
+ // if (_applications.Any(a => a.Key.ApplicationName.ToUpperInvariant().Equals(applicationName)))
87
+ // {
88
+ // ApplicationItem application = _applications.First(a => a.Key.ApplicationName.ToUpperInvariant().Equals(applicationName)).Key;
89
+ // _applications[application] = ApplicationState.None;
90
+ // CallNewLog($"Application stopped: {application}");
91
+ // }
92
+ // }
93
+ // }
94
+
95
+ public void AddProcess ( ApplicationItem application )
89
96
{
90
97
lock ( _applicationsLock )
91
98
{
@@ -94,8 +101,6 @@ public void AddProcess(ApplicationItem application, bool updateRunningProcesses
94
101
_applications . Add ( application , ApplicationState . None ) ;
95
102
CallNewLog ( $ "Application added to process watcher: { application } ") ;
96
103
}
97
- if ( updateRunningProcesses )
98
- UpdateRunningProcessesOnce ( ) ;
99
104
}
100
105
}
101
106
@@ -119,9 +124,8 @@ public void Start()
119
124
lock ( _accessLock )
120
125
{
121
126
CallNewLog ( $ "Starting process watcher...") ;
122
- startWatch . Start ( ) ;
123
- stopWatch . Start ( ) ;
124
- UpdateRunningProcessesOnce ( ) ;
127
+ //startWatch.Start();
128
+ //stopWatch.Start();
125
129
_isRunning = true ;
126
130
_watchProcessThread = new Thread ( WatchProcessLoop ) ;
127
131
_watchProcessThread . IsBackground = true ;
@@ -137,9 +141,8 @@ public void Stop()
137
141
lock ( _accessLock )
138
142
{
139
143
CallNewLog ( $ "Stopping process watcher...") ;
140
- startWatch . Stop ( ) ;
141
- stopWatch . Stop ( ) ;
142
-
144
+ //startWatch.Stop();
145
+ //stopWatch.Stop();
143
146
_stopRequested = true ;
144
147
_watchProcessThread . Join ( ) ;
145
148
_stopRequested = false ;
@@ -149,36 +152,6 @@ public void Stop()
149
152
}
150
153
}
151
154
152
-
153
- private void UpdateRunningProcessesOnce ( )
154
- {
155
- lock ( _applicationsLock )
156
- {
157
- CallNewLog ( $ "Looking for running applications on start...") ;
158
-
159
- Process [ ] processes = Process . GetProcesses ( ) ;
160
-
161
- List < ApplicationItem > applications = _applications . Select ( a => a . Key ) . ToList ( ) ;
162
- foreach ( ApplicationItem application in applications )
163
- {
164
- _applications [ application ] = ApplicationState . None ;
165
- foreach ( var process in processes . Select ( p => p . ProcessName ) )
166
- {
167
- if ( process . ToUpperInvariant ( ) == application . ApplicationName . ToUpperInvariant ( ) )
168
- {
169
- _applications [ application ] = ApplicationState . Running ;
170
- CurrentRunningApplicationItem = application ;
171
- CallNewLog ( $ "Application is running: { application } ") ;
172
- }
173
- }
174
- }
175
- CallNewLog ( $ "No application is running.") ;
176
-
177
- CurrentRunningApplicationItem = null ;
178
- }
179
- }
180
-
181
-
182
155
private void WatchProcessLoop ( )
183
156
{
184
157
while ( ! _stopRequested )
@@ -207,11 +180,33 @@ private void WatchProcessLoop()
207
180
}
208
181
}
209
182
210
-
183
+
211
184
212
185
213
186
private bool GetIsOneProcessRunning ( )
214
187
{
188
+
189
+ lock ( _applicationsLock )
190
+ {
191
+
192
+ Process [ ] processes = Process . GetProcesses ( ) ;
193
+
194
+ List < ApplicationItem > applications = _applications . Select ( a => a . Key ) . ToList ( ) ;
195
+ foreach ( ApplicationItem application in applications )
196
+ {
197
+ _applications [ application ] = ApplicationState . None ;
198
+ foreach ( var process in processes . Select ( p => p . ProcessName ) )
199
+ {
200
+ if ( process . ToUpperInvariant ( ) == application . ApplicationName . ToUpperInvariant ( ) )
201
+ {
202
+ _applications [ application ] = ApplicationState . Running ;
203
+ CurrentRunningApplicationItem = application ;
204
+ }
205
+ }
206
+ }
207
+ CurrentRunningApplicationItem = null ;
208
+ }
209
+
215
210
if ( _applications . Any ( a => a . Value != ApplicationState . None ) )
216
211
{
217
212
var application = _applications . First ( a => a . Value != ApplicationState . None ) ;
0 commit comments