Skip to content

Commit f8754bd

Browse files
committed
Add support for cancellation in executing script
1 parent b698683 commit f8754bd

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

src/UserInputMacro/AppEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Windows;
34
using System.Windows.Interop;
45
using System.Windows.Media;
@@ -21,6 +22,7 @@ class AppEnvironment
2122
public bool IsConsoleMode { get; set; } = true;
2223
public double DpiWidth { get; internal set; } = 1.0;
2324
public double DpiHeight { get; internal set; } = 1.0;
25+
public CancellationToken CancelToken { get; set; }
2426

2527
private AppEnvironment()
2628
{

src/UserInputMacro/MacroScript.cs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public class MacroScript
1212
public async Task Delay( int millsecond )
1313
{
1414
try {
15-
await Task.Delay( millsecond );
15+
await Task.Delay( millsecond, AppEnvironment.GetInstance().CancelToken );
16+
}
17+
catch( TaskCanceledException ) {
18+
throw;
1619
}
1720
catch( Exception ex ) {
1821
await CommonUtil.HandleExceptionAsync( ex );
@@ -29,6 +32,9 @@ public async Task PressKey( ushort virtualKey )
2932

3033
await SendKeyInput( input.ToArray() );
3134
}
35+
catch( TaskCanceledException ) {
36+
throw;
37+
}
3238
catch( Exception ex ) {
3339
await CommonUtil.HandleExceptionAsync( ex );
3440
}
@@ -44,6 +50,9 @@ public async Task ReleaseKey( ushort virtualKey )
4450

4551
await SendKeyInput( input.ToArray() );
4652
}
53+
catch( TaskCanceledException ) {
54+
throw;
55+
}
4756
catch( Exception ex ) {
4857
await CommonUtil.HandleExceptionAsync( ex );
4958
}
@@ -54,6 +63,9 @@ public async Task SetMousePos( int x, int y )
5463
try {
5564
await GetSingleMouseEventTask( x, y, MouseEvent.Move );
5665
}
66+
catch( TaskCanceledException ) {
67+
throw;
68+
}
5769
catch( Exception ex ) {
5870
await CommonUtil.HandleExceptionAsync( ex );
5971
}
@@ -64,6 +76,9 @@ public async Task PushLeftButton( int x, int y )
6476
try {
6577
await GetSingleMouseEventTask( x, y, MouseEvent.LeftDown );
6678
}
79+
catch( TaskCanceledException ) {
80+
throw;
81+
}
6782
catch( Exception ex ) {
6883
await CommonUtil.HandleExceptionAsync( ex );
6984
}
@@ -74,6 +89,9 @@ public async Task PullLeftButton( int x, int y )
7489
try {
7590
await GetSingleMouseEventTask( x, y, MouseEvent.LeftUp );
7691
}
92+
catch( TaskCanceledException ) {
93+
throw;
94+
}
7795
catch( Exception ex ) {
7896
await CommonUtil.HandleExceptionAsync( ex );
7997
}
@@ -84,6 +102,9 @@ public async Task PushMiddleButton( int x, int y )
84102
try {
85103
await GetSingleMouseEventTask( x, y, MouseEvent.MiddleDown );
86104
}
105+
catch( TaskCanceledException ) {
106+
throw;
107+
}
87108
catch( Exception ex ) {
88109
await CommonUtil.HandleExceptionAsync( ex );
89110
}
@@ -94,6 +115,9 @@ public async Task PullMiddleButton( int x, int y )
94115
try {
95116
await GetSingleMouseEventTask( x, y, MouseEvent.MiddleUp );
96117
}
118+
catch( TaskCanceledException ) {
119+
throw;
120+
}
97121
catch( Exception ex ) {
98122
await CommonUtil.HandleExceptionAsync( ex );
99123
}
@@ -104,6 +128,9 @@ public async Task PushRightButton( int x, int y )
104128
try {
105129
await GetSingleMouseEventTask( x, y, MouseEvent.RightDown );
106130
}
131+
catch( TaskCanceledException ) {
132+
throw;
133+
}
107134
catch( Exception ex ) {
108135
await CommonUtil.HandleExceptionAsync( ex );
109136
}
@@ -114,6 +141,9 @@ public async Task PullRightButton( int x, int y )
114141
try {
115142
await GetSingleMouseEventTask( x, y, MouseEvent.RightUp );
116143
}
144+
catch( TaskCanceledException ) {
145+
throw;
146+
}
117147
catch( Exception ex ) {
118148
await CommonUtil.HandleExceptionAsync( ex );
119149
}
@@ -129,6 +159,9 @@ public async Task WheelMouse( int x, int y, int wheelRotate )
129159

130160
await SendMouseInput( input.ToArray() );
131161
}
162+
catch( TaskCanceledException ) {
163+
throw;
164+
}
132165
catch( Exception ex ) {
133166
await CommonUtil.HandleExceptionAsync( ex );
134167
}
@@ -144,6 +177,9 @@ public async Task HWheelMouse( int x, int y, int wheelRotate )
144177

145178
await SendMouseInput( input.ToArray() );
146179
}
180+
catch( TaskCanceledException ) {
181+
throw;
182+
}
147183
catch( Exception ex ) {
148184
await CommonUtil.HandleExceptionAsync( ex );
149185
}
@@ -152,10 +188,12 @@ public async Task HWheelMouse( int x, int y, int wheelRotate )
152188
public async Task SetMode( byte mode )
153189
{
154190
try {
155-
await Task.Run( () =>
156-
{
191+
await Task.Run( () => {
157192
AppEnvironment.GetInstance().Mode = ( ModeKind ) mode;
158-
} );
193+
}, AppEnvironment.GetInstance().CancelToken );
194+
}
195+
catch( TaskCanceledException ) {
196+
throw;
159197
}
160198
catch( Exception ex ) {
161199
await CommonUtil.HandleExceptionAsync( ex );
@@ -165,7 +203,10 @@ await Task.Run( () =>
165203
public async Task WrileUserCustomLog( Dictionary<string, string> userCustomDic )
166204
{
167205
try {
168-
await Logger.WriteUserCustomAsync( userCustomDic );
206+
await Task.Run( () => Logger.WriteUserCustomAsync( userCustomDic ), AppEnvironment.GetInstance().CancelToken );
207+
}
208+
catch( TaskCanceledException ) {
209+
throw;
169210
}
170211
catch( Exception ex ) {
171212
await CommonUtil.HandleExceptionAsync( ex );
@@ -233,7 +274,7 @@ private async Task SendMouseInput( MouseInput[] mouseInput )
233274
}
234275

235276
if( !CommonUtil.CheckMode( ModeKind.KeyOnly ) ) {
236-
await Task.Run( () => SendInputWrapper.SendMouseInput( mouseInput ) );
277+
await Task.Run( () => SendInputWrapper.SendMouseInput( mouseInput ), AppEnvironment.GetInstance().CancelToken );
237278
}
238279
}
239280

@@ -244,7 +285,7 @@ private async Task SendKeyInput( KeyInput[] keyInput )
244285
}
245286

246287
if( !CommonUtil.CheckMode( ModeKind.MouseOnly ) ) {
247-
await Task.Run( () => SendInputWrapper.SendKeyInput( keyInput ) );
288+
await Task.Run( () => SendInputWrapper.SendKeyInput( keyInput ), AppEnvironment.GetInstance().CancelToken );
248289
}
249290
}
250291
}

src/UserInputMacro/MainWindowViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading;
34
using System.Windows.Threading;
45
using System.Threading.Tasks;
56
using System.ComponentModel;
@@ -19,6 +20,7 @@ class MainWindowViewModel : INotifyPropertyChanged
1920
private static readonly PropertyChangedEventArgs scriptPathChangedEventArgs = new PropertyChangedEventArgs( nameof( ScriptPath ) );
2021
private static readonly PropertyChangedEventArgs errorMessageChangedEventArgs = new PropertyChangedEventArgs( nameof( ErrorMessage ) );
2122

23+
private CancellationTokenSource cancelTokenSrc;
2224
public event PropertyChangedEventHandler PropertyChanged;
2325

2426
public DelegateCommand RecordCommand { get; set; }
@@ -99,20 +101,24 @@ private async Task PlayCmd_ExecuteAsync()
99101

100102
try {
101103
buttonState.IsPlaying = true;
102-
WinDispacher?.Invoke( new Action( CommandManager.InvalidateRequerySuggested ) );
104+
cancelTokenSrc = new CancellationTokenSource();
105+
AppEnvironment.GetInstance().CancelToken = cancelTokenSrc.Token;
103106

107+
WinDispacher?.Invoke( new Action( CommandManager.InvalidateRequerySuggested ) );
104108
await ScriptExecuter.ExecuteAsync( ScriptPath );
105109
}
106110
catch( CompilationErrorException ex ) {
107111
ErrorMessage = "[Compile Error]" + Environment.NewLine + ex.Message;
108112
}
113+
catch( TaskCanceledException ) {
114+
ErrorMessage = "[Message]Script was cancelled.";
115+
}
109116
catch( Exception ) {
110117
throw;
111118
}
112119

113120
buttonState.IsPlaying = false;
114121
WinDispacher?.Invoke( new Action( CommandManager.InvalidateRequerySuggested ) );
115-
116122
}
117123

118124
private void StopCmd_Execute()
@@ -123,7 +129,7 @@ private void StopCmd_Execute()
123129
SaveMacroScript();
124130
}
125131
else if( buttonState.IsPlaying ) {
126-
buttonState.IsPlaying = false;
132+
cancelTokenSrc.Cancel();
127133
}
128134
}
129135

0 commit comments

Comments
 (0)