Reduce task wait/timeout code in AsyncTCPConnector

This commit is contained in:
John Colvin committed 2024-10-30 19:31:09 +00:00
1 parent aae50cd5f8
commit d439e3e695
1 file changed
+2 -14
@@ -45,20 +45,8 @@ namespace bts {
// wais for wait_before_retrying before trying again
public static void _keep_trying_blocking_function(Action action, TimeSpan timeout, TimeSpan wait_before_retrying) {
Func<bool> try_once = () => {
var cts = new CancellationTokenSource();
var token = cts.Token;
var task = Task.Run(action, token);
if (Task.WaitAny(task, Task.Delay(timeout, token)) == 0) {
// Task completed within timeout
task.GetAwaiter().GetResult(); // Ensure any exceptions/cancellation are rethrown
return true;
} else {
// Timeout
cts.Cancel();
return false;
}
var task = Task.Run(action);
return Task.WhenAny(task, Task.Delay(timeout)).GetAwaiter().GetResult() == task && task.IsCompletedSuccessfully;
};
bool connected = false;
while (!connected) {