Click here to Skip to main content
15,566,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,

C#
private void SQLClientLoader_Load(object sender, EventArgs e)
        {
            this.GetSQLState();
        }

C#
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;
        }

C#
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.
Posted

1 solution

So yes I finally found the solution after 2 weeks.
I just need to clear the specific connection pool.
Then everything run as smooth as oh wow made my day.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900