Skip to content
This repository was archived by the owner on Apr 21, 2021. It is now read-only.

Commit 2c172b6

Browse files
committed
cleanup
1 parent 5872365 commit 2c172b6

File tree

5 files changed

+137
-132
lines changed

5 files changed

+137
-132
lines changed

src/EventHook/ApplicationWatcher.cs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,64 +37,74 @@ public class ApplicationEventArgs : EventArgs
3737
public class ApplicationWatcher
3838
{
3939
/*Application history*/
40-
static AsyncCollection<object> _appQueue;
41-
static public bool AppRun;
42-
static private List<WindowData> _activeWindows;
43-
static private DateTime _prevTimeApp;
40+
private static object _Accesslock = new object();
41+
private static bool _IsRunning;
42+
43+
private static AsyncCollection<object> _appQueue;
44+
45+
private static List<WindowData> _activeWindows;
46+
private static DateTime _prevTimeApp;
4447

4548
public static event EventHandler<ApplicationEventArgs> OnApplicationWindowChange;
4649

4750
public static void Start()
4851
{
52+
if (!_IsRunning)
53+
lock (_Accesslock)
54+
{
55+
_activeWindows = new List<WindowData> { };
56+
_prevTimeApp = DateTime.Now;
4957

50-
_activeWindows = new List<WindowData> { };
51-
_prevTimeApp = DateTime.Now;
58+
_appQueue = new AsyncCollection<object>();
5259

53-
_appQueue = new AsyncCollection<object>();
60+
var handler = SharedMessagePump.GetHandle();
5461

55-
var handler = SharedMessagePump.GetHandle();
56-
57-
Task.Factory.StartNew(() => { }).ContinueWith(x =>
58-
{
59-
WindowHook.WindowCreated += new GeneralShellHookEventHandler(WindowCreated);
60-
WindowHook.WindowDestroyed += new GeneralShellHookEventHandler(WindowDestroyed);
61-
WindowHook.WindowActivated += new GeneralShellHookEventHandler(WindowActivated);
62+
Task.Factory.StartNew(() => { }).ContinueWith(x =>
63+
{
64+
WindowHook.WindowCreated += new GeneralShellHookEventHandler(WindowCreated);
65+
WindowHook.WindowDestroyed += new GeneralShellHookEventHandler(WindowDestroyed);
66+
WindowHook.WindowActivated += new GeneralShellHookEventHandler(WindowActivated);
6267

63-
}, SharedMessagePump.GetTaskScheduler());
68+
}, SharedMessagePump.GetTaskScheduler());
6469

65-
_lastEventWasLaunched = false;
66-
_lastHwndLaunched = IntPtr.Zero;
70+
_lastEventWasLaunched = false;
71+
_lastHwndLaunched = IntPtr.Zero;
6772

68-
Task.Factory.StartNew(() => AppConsumer());
69-
AppRun = true;
73+
Task.Factory.StartNew(() => AppConsumer());
74+
_IsRunning = true;
75+
}
7076

7177
}
7278
public static void Stop()
7379
{
74-
WindowHook.WindowCreated -= new GeneralShellHookEventHandler(WindowCreated);
75-
WindowHook.WindowDestroyed -= new GeneralShellHookEventHandler(WindowDestroyed);
76-
WindowHook.WindowActivated -= new GeneralShellHookEventHandler(WindowActivated);
80+
if (_IsRunning)
81+
lock (_Accesslock)
82+
{
83+
WindowHook.WindowCreated -= new GeneralShellHookEventHandler(WindowCreated);
84+
WindowHook.WindowDestroyed -= new GeneralShellHookEventHandler(WindowDestroyed);
85+
WindowHook.WindowActivated -= new GeneralShellHookEventHandler(WindowActivated);
7786

78-
_appQueue.Add(false);
79-
AppRun = false;
87+
_appQueue.Add(false);
88+
_IsRunning = false;
89+
}
8090

8191
}
82-
static void WindowCreated(ShellHook shellObject, IntPtr hWnd)
92+
private static void WindowCreated(ShellHook shellObject, IntPtr hWnd)
8393
{
8494
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 0 });
8595
}
86-
static void WindowDestroyed(ShellHook shellObject, IntPtr hWnd)
96+
private static void WindowDestroyed(ShellHook shellObject, IntPtr hWnd)
8797
{
8898
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 2 });
8999
}
90-
static void WindowActivated(ShellHook shellObject, IntPtr hWnd)
100+
private static void WindowActivated(ShellHook shellObject, IntPtr hWnd)
91101
{
92102
_appQueue.Add(new WindowData() { HWnd = hWnd, EventType = 1 });
93103
}
94104
// This is the method to run when the timer is raised.
95-
static private async Task AppConsumer()
105+
private static async Task AppConsumer()
96106
{
97-
while (AppRun)
107+
while (_IsRunning)
98108
{
99109
//blocking here until a key is added to the queue
100110
var item = await _appQueue.TakeAsync();
@@ -116,8 +126,8 @@ static private async Task AppConsumer()
116126

117127
}
118128
}
119-
static IntPtr _lastHwndLaunched;
120-
static void WindowCreated(WindowData wnd)
129+
private static IntPtr _lastHwndLaunched;
130+
private static void WindowCreated(WindowData wnd)
121131
{
122132

123133
_activeWindows.Add(wnd);
@@ -143,7 +153,7 @@ private static void ApplicationStatus(WindowData wnd, ApplicationEvents appEvent
143153
}
144154
}
145155

146-
static void WindowDestroyed(WindowData wnd)
156+
private static void WindowDestroyed(WindowData wnd)
147157
{
148158

149159
if (_activeWindows.Any(x => x.HWnd == wnd.HWnd))
@@ -153,8 +163,8 @@ static void WindowDestroyed(WindowData wnd)
153163
}
154164
_lastEventWasLaunched = false;
155165
}
156-
static bool _lastEventWasLaunched;
157-
static void WindowActivated(WindowData wnd)
166+
private static bool _lastEventWasLaunched;
167+
private static void WindowActivated(WindowData wnd)
158168
{
159169

160170
if (_activeWindows.Any(x => x.HWnd == wnd.HWnd))

src/EventHook/ClipboardWatcher.cs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,61 +29,68 @@ public class ClipboardEventArgs : EventArgs
2929
public class ClipboardWatcher
3030
{
3131
/*Clip board monitor*/
32+
public static bool _IsRunning;
33+
private static object _Accesslock = new object();
34+
3235
private static ClipBoardHook _clip;
3336
private static AsyncCollection<object> _clipQueue;
34-
public static bool ClipRun;
35-
public static event EventHandler<ClipboardEventArgs> OnClipboardModified;
3637

38+
public static event EventHandler<ClipboardEventArgs> OnClipboardModified;
3739

3840
public static void Start()
3941
{
42+
if (!_IsRunning)
43+
lock (_Accesslock)
44+
{
45+
try
46+
{
47+
_clipQueue = new AsyncCollection<object>();
4048

41-
try
42-
{
43-
_clipQueue = new AsyncCollection<object>();
44-
49+
Task.Factory.StartNew(() => { }).ContinueWith(x =>
50+
{
51+
_clip = new ClipBoardHook();
52+
_clip.RegisterClipboardViewer();
53+
_clip.ClipBoardChanged += ClipboardHandler;
4554

46-
Task.Factory.StartNew(() => { }).ContinueWith(x =>
47-
{
48-
_clip = new ClipBoardHook();
49-
_clip.RegisterClipboardViewer();
50-
_clip.ClipBoardChanged += ClipboardHandler;
55+
}, SharedMessagePump.GetTaskScheduler());
5156

52-
}, SharedMessagePump.GetTaskScheduler());
57+
Task.Factory.StartNew(() => ClipConsumerAsync());
5358

54-
Task.Factory.StartNew(() => ClipConsumerAsync());
59+
_IsRunning = true;
5560

56-
ClipRun = true;
61+
}
62+
catch
63+
{
64+
if (_clip != null)
65+
{
66+
Stop();
67+
}
5768

58-
}
59-
catch
60-
{
61-
if (_clip != null)
62-
{
63-
Stop();
69+
}
6470
}
6571

66-
67-
}
68-
6972
}
7073
public static void Stop()
7174
{
72-
if (_clip != null)
73-
{
74-
75-
Task.Factory.StartNew(() => { }).ContinueWith(x =>
75+
if (_IsRunning)
76+
lock (_Accesslock)
7677
{
77-
_clip.ClipBoardChanged -= ClipboardHandler;
78-
_clip.UnregisterClipboardViewer();
79-
_clip.Dispose();
78+
if (_clip != null)
79+
{
8080

81-
}, SharedMessagePump.GetTaskScheduler());
81+
Task.Factory.StartNew(() => { }).ContinueWith(x =>
82+
{
83+
_clip.ClipBoardChanged -= ClipboardHandler;
84+
_clip.UnregisterClipboardViewer();
85+
_clip.Dispose();
8286

83-
}
87+
}, SharedMessagePump.GetTaskScheduler());
8488

85-
ClipRun = false;
86-
_clipQueue.Add(false);
89+
}
90+
91+
_IsRunning = false;
92+
_clipQueue.Add(false);
93+
}
8794

8895
}
8996
private static void ClipboardHandler(object sender, EventArgs e)
@@ -93,18 +100,18 @@ private static void ClipboardHandler(object sender, EventArgs e)
93100

94101
private static async Task ClipConsumerAsync()
95102
{
96-
while (ClipRun)
103+
while (_IsRunning)
97104
{
98105
var item = await _clipQueue.TakeAsync();
99106
if (item is bool) break;
100107

101108
ClipboardHandler(item);
102109

103-
110+
104111
}
105112

106113
}
107-
114+
108115
private static void ClipboardHandler(object sender)
109116
{
110117

src/EventHook/KeyboardWatcher.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class KeyboardWatcher
3131
{
3232

3333
/*Keyboard*/
34-
private static bool _KeyboardRun { get; set; }
34+
private static bool _IsRunning { get; set; }
3535
private static KeyboardHook _kh;
3636
private static object _Accesslock = new object();
3737
private static AsyncCollection<object> _kQueue;
@@ -40,11 +40,9 @@ public class KeyboardWatcher
4040

4141
public static void Start()
4242
{
43-
44-
if (!_KeyboardRun)
43+
if (!_IsRunning)
4544
lock (_Accesslock)
4645
{
47-
4846
_kQueue = new AsyncCollection<object>();
4947

5048
_kh = new KeyboardHook();
@@ -59,16 +57,15 @@ public static void Start()
5957

6058
Task.Factory.StartNew(() => ConsumeKeyAsync());
6159

62-
_KeyboardRun = true;
60+
_IsRunning = true;
6361
}
6462

6563
}
6664
public static void Stop()
6765
{
68-
if (_KeyboardRun)
66+
if (_IsRunning)
6967
lock (_Accesslock)
7068
{
71-
7269
if (_kh != null)
7370
{
7471
_kh.KeyDown -= new RawKeyEventHandler(KListener);
@@ -77,12 +74,10 @@ public static void Stop()
7774
}
7875

7976
_kQueue.Add(false);
80-
_KeyboardRun = false;
81-
77+
_IsRunning = false;
8278
}
8379
}
8480

85-
8681
private static void KListener(object sender, RawKeyEventArgs e)
8782
{
8883
_kQueue.Add(new KeyData() { UnicodeCharacter = e.Character, Keyname = e.Key.ToString(), EventType = (KeyEvent)e.EventType });
@@ -91,15 +86,14 @@ private static void KListener(object sender, RawKeyEventArgs e)
9186
// This is the method to run when the timer is raised.
9287
private static async Task ConsumeKeyAsync()
9388
{
94-
while (_KeyboardRun)
89+
while (_IsRunning)
9590
{
9691

9792
//blocking here until a key is added to the queue
9893
var item = await _kQueue.TakeAsync();
9994
if (item is bool) break;
10095

10196
KListener_KeyDown((KeyData)item);
102-
10397
}
10498
}
10599

@@ -113,8 +107,5 @@ private static void KListener_KeyDown(KeyData kd)
113107

114108
}
115109

116-
117110
}
118-
119-
120111
}

0 commit comments

Comments
 (0)