Waiting to Start a Service

J

Jeff

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
 
M

[MSFT]

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
 
J

Jeff

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
 
M

[MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top