Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cancellable wait or find
#1
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?
#2
Can you provide one or more examples where it can be useful?

Maybe this can be used instead:
Code:
Copy      Help
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.
#3
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.
#4
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.
 
Code:
Copy      Help
var cts = new CancellationTokenSource(2000);
try { wait.until(new Seconds(0) { Cancel = cts.Token }, () => false); }
catch (OperationCanceledException) { print.it("canceled"); }

Code:
Copy      Help
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"); }
}
#5
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
#6
https://www.libreautomate.com/forum/show...p?tid=7557
#7
Exactly what I needed! Thank you so much.


Forum Jump:


Users browsing this thread: 1 Guest(s)