Posts: 1,031
Threads: 246
Joined: Jul 2022
03-28-2024, 09:10 AM
(This post was last modified: 03-28-2024, 09:11 AM by Davider.)
Recently, some of the issues I encountered seem to be related to execution speed. When errors occur, simply retrying once can resolve them.
Could we add a retry count attribute for certain functions as shown in the diagram below?
Although achievable with try...catch, it's a bit cumbersome.
Posts: 12,073
Threads: 140
Joined: Dec 2002
Try Polly.
Get:
https://www.nuget.org/packages/Polly.Core/
Learn:
https://www.pollydocs.org/
Example:
// script "nuget Polly.cs"
/*/ nuget -\Polly.Core; /*/
using Polly;
using Polly.Retry;
//Create Polly object that executes "retry".
var ro = new RetryStrategyOptions(); //default options: max 3 retries, every 2 s
//var ro = new RetryStrategyOptions { MaxRetryAttempts = 10, Delay = TimeSpan.FromMilliseconds(100) }; //custom options
var retry = new ResiliencePipelineBuilder().AddRetry(ro).Build();
//example. Find window. On exception wait/retry several times.
var w = retry.Execute(() => wnd.find(0, "*Notepad"));
print.it(w);
//example. Activate window. On exception wait/retry several times.
retry.Execute(() => { w.Activate(); });
Posts: 1,031
Threads: 246
Joined: Jul 2022
03-28-2024, 10:59 PM
(This post was last modified: 03-28-2024, 11:14 PM by Davider.)
Thank you for sharing. It's very useful.
It would be very convenient if the details were hidden, for example, by directly using the following code,
wnd.find(0, "*Notepad").retry(2, 300); //retry 2 times. every 300ms
Posts: 12,073
Threads: 140
Joined: Dec 2002
Many LA functions already have "retry" parameters. It's the Seconds wait parameter, eg in wnd.find. You can post a list of other functions that should have such parameter.
One alternative - use universal "retry" functions, for example Polly.
Other alternative - LA could have a command "Generate retry code with try/catch" in menu Edit > Surround.
Posts: 12,073
Threads: 140
Joined: Dec 2002
03-29-2024, 08:19 AM
(This post was last modified: 03-29-2024, 08:43 AM by Gintaras.)
In next LA added
wait.retry. It executes your code and waits/retries while it throws an exception.
string file = @"C:\Test\test.txt";
string s = wait.retry(5, () => File.ReadAllText(file));
s = s.Upper();
wait.retry(5, () => { File.WriteAllText(file, s); });
print.it("ok");
wait.retry(new(5) { Period = 100 }, () => { File.WriteAllText(file, s); });
wait.retry(5, () => { File.WriteAllText(file, s); }, e => e is IOException);