Posts: 8
Threads: 3
Joined: Aug 2023
Hi, I have an idea to use CancellationToken.WaitHandle to perform wait in wait class or any find operation (uiimage, orc, etc...), so we can cancel the wait manually without terminate the process.
Will it become true in further update?
Posts: 12,073
Threads: 140
Joined: Dec 2002
Can you provide one or more examples where it can be useful?
Maybe this can be used instead:
var cts = new CancellationTokenSource(1000);
Task.Run(() => { 3.s(); }).Wait(cts.Token);
I don't want to add rarely used parameters (CancellationToken) to these functions.
Posts: 8
Threads: 3
Joined: Aug 2023
I have a case like: Wait for an image, with timeout of 180 seconds. When I cancel that task(but do not want to exit process), the imageFinder still execute until reach the timeout. So it does not actually cancel.
Posts: 12,073
Threads: 140
Joined: Dec 2002
10-10-2023, 05:46 AM
(This post was last modified: 10-10-2023, 06:04 AM by Gintaras.)
Added in v0.18. Thank you.
The type of the timeout parameter of wait functions now is
Seconds instead of double. It allows to pass wait options and cancellation token.
var cts = new CancellationTokenSource(2000);
try { wait.until(new Seconds(0) { Cancel = cts.Token }, () => false); }
catch (OperationCanceledException) { print.it("canceled"); }
CancellationTokenSource cts = null;
int iTask = 0;
var b = new wpfBuilder("Window").WinSize(400);
b.R.AddButton("Start", _ => { cts?.Cancel(); cts = new(); Task.Run(_Task); });
b.R.AddButton("Cancel", _ => { cts?.Cancel(); });
b.R.AddOkCancel();
b.End();
b.Window.Closed += (_, _) => { cts?.Cancel(); };
if (!b.ShowDialog()) return;
void _Task() {
using var osd = osdText.showTransparentText($"Waiting, task {++iTask}", -1, xy: new(y: .6f));
try { wait.until(new Seconds(0) { Cancel = cts.Token }, () => false); }
catch (OperationCanceledException) { print.it("canceled"); }
}
Posts: 8
Threads: 3
Joined: Aug 2023
Nice, Greate work! I'll update my usage to this new approach.
Btw, I use wnd.findOrRun to find a Window (for example, call it 'A' program, it is already opened, so this was actually 'find'). After I finished all tasks, I exit my app. But file "AuCpp.dll" is being used by that A program and I cannot delete/move/replace it until I close that 'A' program.
I's using LibreAutomate nuget, version 0.18
Posts: 12,073
Threads: 140
Joined: Dec 2002
Posts: 8
Threads: 3
Joined: Aug 2023
Exactly what I needed! Thank you so much.