I'm trying to create a wait form that will appear when sql connection is not available (ex. server shutted down / lan network disabled) the wait form will keep appearing until the connection is available again, so I don't need to keep executing query to check if the connection is available or not.
Here is what I do,
private void SQLClientLoader_Load(object sender, EventArgs e)
{
this.GetSQLState();
}
async private System.Threading.Tasks.Task<ConnectionState> GetStateAsync()
{
do
{
this.cts = new CancellationTokenSource();
this.cnn = new SqlConnection(this.par.Constr);
try
{
await Task.Delay(2500);
await this.cnn.OpenAsync(cts.Token);
}
catch (Exception ex)
{
cts.Cancel(false);
this.cnn.Dispose();
this.cts.Dispose();
}
} while (this.cnn.State != ConnectionState.Open);
return this.cnn.State;
}
async private void GetSQLState()
{
if (await GetStateAsync() == ConnectionState.Open)
{
this.Close();
}
await Task.Delay(2500);
}
This code is working, but if the wait form looped for more than 5 times, it takes a while before it close itself.
The longer the server unavailable, the longer the form will wait before it closes.
So I tried to put breakpoint inside the loop when the connection is available, it can't open the connection, it's like the cnn.OpenAsync() queue up.
Anything wrong in my code?
Note : I stop and start the sql instance service to try this code.