By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

Waiting to Start a Service

P: n/a
Hi -

I'm building a VB.NET Windows application with an MSDE database.
Anticipating the possibility that the MSDE service may have been stopped
outside of my app, I'm trying to code a way to start it when my app is
launched. I can't seem to get the code to wait for MSDE to start before
continuing (the next step is opening a connection). My development machine
is WinXP Pro.

I'm using the ServiceController class to start and check the service. When
my code (see below) runs, the ex3 exception is never raised. The MessageBox
(MessageBox.Show(strStat)) Shows "Running" so the service started
successfully. If I click the MessageBox OK button after about 5 seconds,
the ensuing connection succeeds. If I click the MessageBox button without
waiting, the ensuing connection fails.

What am I doing wrong and/or what's the right way to do this??

Thanks for your help.

Here's my code:

Dim strStat As String = "Unknown"
Dim svc As New ServiceController
svc.ServiceName = "MSSQL$" & strInstance

If svc.Status = ServiceControllerStatus.Stopped Then
Try
svc.Start()
svc.WaitForStatus(ServiceControllerStatus.Running)

Catch ex3 As Exception
MessageBox.Show("Unable to start database service.",
strAppName, MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End If

If svc.Status = ServiceControllerStatus.Running Then
strStat = "Running"
ElseIf svc.Status = ServiceControllerStatus.Stopped Then
strStat = "Stopped"
End If

MessageBox.Show(strStat)

frmMain.conSC = New SqlConnection(strConn)
frmMain.conSC.ConnectionString = strConn
frmMain.conSC.Open()
- Jeff
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Jeff,

The code seems to be OK. Based on my experience, even the status is
"running", MSDE service still may be executing some initializtion jobs and
refuse to outside connection. I suggest you may put the Connection.open in
a loop, when found it failed, wait for seconds and then open the connection
again. This should be a proper solution for the problem.

Luke

Nov 21 '05 #2

P: n/a
OK Luke -

I've resolved the problem by looping/sleeping until a connection succeeds.

But shouldn't there be a way for code to determine when MSDE is able to
_accept_ a connection?? Without that, it seems to me that knowing that
Status is Running is pretty worthless.

- Jeff
"[MSFT]" <lu******@online.microsoft.com> wrote in message
news:AY**************@cpmsftngxa10.phx.gbl...
Hi Jeff,

The code seems to be OK. Based on my experience, even the status is
"running", MSDE service still may be executing some initializtion jobs and refuse to outside connection. I suggest you may put the Connection.open in
a loop, when found it failed, wait for seconds and then open the connection again. This should be a proper solution for the problem.

Luke

Nov 21 '05 #3

P: n/a
The status property is not believable during the MSDE service's Statup
process. Unfortunately, it is the only property we can access. The only way
is to try connection till success.

Luke

Nov 21 '05 #4

P: n/a
Thanks Luke.

"[MSFT]" <lu******@online.microsoft.com> wrote in message
news:ra**************@cpmsftngxa10.phx.gbl...
The status property is not believable during the MSDE service's Statup
process. Unfortunately, it is the only property we can access. The only way is to try connection till success.

Luke

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.