@@ -24,7 +24,14 @@ public class ProcessWatcher
2424 public ApplicationItem CurrentFocusedApplicationItem { get ; private set ; }
2525
2626 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+ }
2835
2936 bool _stopRequested = false ;
3037 bool _isRunning = false ;
@@ -34,17 +41,17 @@ public class ProcessWatcher
3441 public event EventHandler OneProcessIsRunningChanged ;
3542 public event EventHandler OneProcessIsFocusedChanged ;
3643
37- ManagementEventWatcher startWatch ;
38- ManagementEventWatcher stopWatch ;
44+ // ManagementEventWatcher startWatch;
45+ // ManagementEventWatcher stopWatch;
3946
4047 public ProcessWatcher ( )
4148 {
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;
4855
4956 }
5057
@@ -55,37 +62,37 @@ private void CallNewLog(string logMessage)
5562 }
5663
5764
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 )
8996 {
9097 lock ( _applicationsLock )
9198 {
@@ -94,8 +101,6 @@ public void AddProcess(ApplicationItem application, bool updateRunningProcesses
94101 _applications . Add ( application , ApplicationState . None ) ;
95102 CallNewLog ( $ "Application added to process watcher: { application } ") ;
96103 }
97- if ( updateRunningProcesses )
98- UpdateRunningProcessesOnce ( ) ;
99104 }
100105 }
101106
@@ -119,9 +124,8 @@ public void Start()
119124 lock ( _accessLock )
120125 {
121126 CallNewLog ( $ "Starting process watcher...") ;
122- startWatch . Start ( ) ;
123- stopWatch . Start ( ) ;
124- UpdateRunningProcessesOnce ( ) ;
127+ //startWatch.Start();
128+ //stopWatch.Start();
125129 _isRunning = true ;
126130 _watchProcessThread = new Thread ( WatchProcessLoop ) ;
127131 _watchProcessThread . IsBackground = true ;
@@ -137,9 +141,8 @@ public void Stop()
137141 lock ( _accessLock )
138142 {
139143 CallNewLog ( $ "Stopping process watcher...") ;
140- startWatch . Stop ( ) ;
141- stopWatch . Stop ( ) ;
142-
144+ //startWatch.Stop();
145+ //stopWatch.Stop();
143146 _stopRequested = true ;
144147 _watchProcessThread . Join ( ) ;
145148 _stopRequested = false ;
@@ -149,36 +152,6 @@ public void Stop()
149152 }
150153 }
151154
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-
182155 private void WatchProcessLoop ( )
183156 {
184157 while ( ! _stopRequested )
@@ -207,11 +180,33 @@ private void WatchProcessLoop()
207180 }
208181 }
209182
210-
183+
211184
212185
213186 private bool GetIsOneProcessRunning ( )
214187 {
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+
215210 if ( _applications . Any ( a => a . Value != ApplicationState . None ) )
216211 {
217212 var application = _applications . First ( a => a . Value != ApplicationState . None ) ;
0 commit comments