Click here to Skip to main content
15,887,416 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to generate barcode and qrcode dynamically? Pin
Richard MacCutchan12-Sep-13 21:54
mveRichard MacCutchan12-Sep-13 21:54 
AnswerRe: How to generate barcode and qrcode dynamically? Pin
AngloThaiSolutions7-Oct-13 4:17
AngloThaiSolutions7-Oct-13 4:17 
QuestionConverting a date format Pin
MarkB12312-Sep-13 2:10
MarkB12312-Sep-13 2:10 
AnswerRe: Converting a date format Pin
MarkB12312-Sep-13 2:30
MarkB12312-Sep-13 2:30 
AnswerRe: Converting a date format Pin
V.12-Sep-13 2:56
professionalV.12-Sep-13 2:56 
GeneralRe: Converting a date format Pin
MarkB12312-Sep-13 3:08
MarkB12312-Sep-13 3:08 
GeneralRe: Converting a date format Pin
V.12-Sep-13 3:16
professionalV.12-Sep-13 3:16 
QuestionAuto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 0:06
Midnight Ahri12-Sep-13 0:06 
So I've been stuck here for 1 week.

Everytime I had problem in SQL connection (ex. someone switch of server) my application will show exception message in a messagebox. I don't know when will the connection available except I keep trying to open the connection / execute a query.

So I create a wait form that will appear if connection is unavailable, keep trying to open the connection, and close itself when connection is available again.

To hide the freeze from user, I use backgroundworker.

This is the backgroundworker code
C#
private void StartLoader(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (this.par.sqSuccess) //if no error, means connection is available, stop looping
                {
                    break;
                }
                else
                {
                    i -= 1;
                }

                System.Threading.Thread.Sleep(5000); //report progress every 5 second
            }

This is the backgroundworker progress changed event
C#
this.cnn = new SqlConnection(this.par.Constr);

try
{
    this.cnn.Open(); //SqlConnection

    this.par.sqSuccess = true; //if no error, then I change this variable
}
catch (Exception ex)
{
    this.par.Exception = ex.Message;
}
finally
{
    if (this.cnn != null) { this.cnn.Dispose(); }
}
if (this.par.sqSuccess) { this.Close(); }

After everything is complete, I tried to stop SQL service from services.msc, then I try to connect.
The wait form will appear and keep doing its job.

A few second after I try to connect, I start the service again and the wait form did close, success.
This is the problem, when I wait a little bit longer before I start the service again, the wait form still closed, but it takes a while.

After I check everything, it seems like the cnn.open() queue up and the longer I stop the service, the longer it takes for the wait form to close.

I searched google and try to add Connect Timeout=3; behind my connection string, as I'm sure my thread.sleep(5000) won't make them queue up, but still not working.

I need help to understand how this cnn.open works, why this is not working, what happen in this code that makes I got this problem.
While processing the cnn.open(), did my application execute the code below or wait for it?

Any help is appreciated, thank you. Smile | :)

PS :
I use do while sqSuccess == false looping, I changed it to for just now and I know it makes no difference, so ignore it and the i-=1 too.
SuggestionRe: Auto Connect SQLConnection Pin
Richard MacCutchan12-Sep-13 0:19
mveRichard MacCutchan12-Sep-13 0:19 
QuestionRe: Auto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 3:27
Midnight Ahri12-Sep-13 3:27 
AnswerRe: Auto Connect SQLConnection Pin
Richard MacCutchan12-Sep-13 3:32
mveRichard MacCutchan12-Sep-13 3:32 
AnswerRe: Auto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 5:07
Midnight Ahri12-Sep-13 5:07 
QuestionRe: Auto Connect SQLConnection Pin
Midnight Ahri13-Sep-13 13:57
Midnight Ahri13-Sep-13 13:57 
AnswerRe: Auto Connect SQLConnection Pin
Richard MacCutchan13-Sep-13 22:05
mveRichard MacCutchan13-Sep-13 22:05 
AnswerRe: Auto Connect SQLConnection Pin
Midnight Ahri14-Sep-13 20:12
Midnight Ahri14-Sep-13 20:12 
AnswerRe: Auto Connect SQLConnection Pin
Pete O'Hanlon12-Sep-13 5:18
mvePete O'Hanlon12-Sep-13 5:18 
AnswerRe: Auto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 5:36
Midnight Ahri12-Sep-13 5:36 
QuestionRe: Auto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 6:45
Midnight Ahri12-Sep-13 6:45 
AnswerRe: Auto Connect SQLConnection Pin
Pete O'Hanlon12-Sep-13 8:13
mvePete O'Hanlon12-Sep-13 8:13 
QuestionRe: Auto Connect SQLConnection Pin
Midnight Ahri12-Sep-13 19:36
Midnight Ahri12-Sep-13 19:36 
QuestionRunning SQL Server 2008 R2 without user interaction Pin
WaseemAmin11-Sep-13 21:49
WaseemAmin11-Sep-13 21:49 
AnswerRe: Running SQL Server 2008 R2 without user interaction Pin
Richard MacCutchan11-Sep-13 22:26
mveRichard MacCutchan11-Sep-13 22:26 
GeneralRe: Running SQL Server 2008 R2 without user interaction Pin
WaseemAmin12-Sep-13 5:54
WaseemAmin12-Sep-13 5:54 
AnswerRe: Running SQL Server 2008 R2 without user interaction Pin
PIEBALDconsult12-Sep-13 3:03
mvePIEBALDconsult12-Sep-13 3:03 
GeneralRe: Running SQL Server 2008 R2 without user interaction Pin
WaseemAmin12-Sep-13 6:02
WaseemAmin12-Sep-13 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.