Currently, I have the code in which the while loop get's stuck when connection.IsConnected = true
private bool TryConnect()
{
if (!connection.IsConnected)
{
Monitor.Enter(_syncRoot);
try
{
while (!connection.IsConnected)
{
Thread.Sleep(100);
}
}
finally
{
Monitor.Exit(_syncRoot);
}
}
return true;
}
So for this, I have tried replacing this with Polly retry but using Polly it does not wait and the log is not added
private bool TryConnect()
{
if (!connection.IsConnected)
{
Monitor.Enter(_syncRoot);
try
{
var policy = Policy.HandleResult<bool>(r => r == false)
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
(ex, time) =>
{
Log.LogInformation(
"retry {TimeOut}s");
}
);
policy.Execute(() => connection.IsConnected);
}
finally
{
Monitor.Exit(_syncRoot);
}
}
return true;
}
If someone can let me know, what I am doing wrong with the code. It will be really helpful