Stopping a Windows Service during OnStart()

D

Dave Wright

Hi,

I am implementing a service that connects to an SMS Controller. We are
implementing this using an assembly in the OnStart() method of a Windows
service. This is working fine, however I cannot work out how to get the
service to stop if we cannot connect to the SMSC, if it is down or incorrect
credentials etc. Currently I am using a ServiceController class which I am
calling the stop method of, but this seems to hang and then eventually stop
the service after the timeout.

Is there any way to abort the OnStart method?

Cheers

Dave Wright
Microsoft Solutions Engineer
3
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

I haven't tried this, but have you thought of throwing an exception?
What happens if you do this?
 
M

Mike Blamires

Throwing an exception will stop the service from running ie.

//Service.cs
protected override void OnStart(string[] args) {
Throw throwEx = new Throw();
}

//Throw.cs
public Throw() {
string dummy = null;
dummy.Substring(10, 3);
}


This will throw an "Could not start the service" when the service starts.
Does anyone know of any resources on this kind of thing, all documentation
and examples I have found are a little thin on the ground.

Thanks
Mike



Nicholas Paldino said:
Dave,

I haven't tried this, but have you thought of throwing an exception?
What happens if you do this?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave Wright said:
Hi,

I am implementing a service that connects to an SMS Controller. We are
implementing this using an assembly in the OnStart() method of a Windows
service. This is working fine, however I cannot work out how to get the
service to stop if we cannot connect to the SMSC, if it is down or incorrect
credentials etc. Currently I am using a ServiceController class which I am
calling the stop method of, but this seems to hang and then eventually stop
the service after the timeout.

Is there any way to abort the OnStart method?

Cheers

Dave Wright
Microsoft Solutions Engineer
3
 
D

Dave Wright

Cheers for responses. I have tried this and it sort of works, I now get a
dialog box with:

The service started and then stopped. Some services do this when they do not
have any work to do.

This isnt exactly ideal however. Any more thoughts?

Cheers
Dave



Mike Blamires said:
Throwing an exception will stop the service from running ie.

//Service.cs
protected override void OnStart(string[] args) {
Throw throwEx = new Throw();
}

//Throw.cs
public Throw() {
string dummy = null;
dummy.Substring(10, 3);
}


This will throw an "Could not start the service" when the service starts.
Does anyone know of any resources on this kind of thing, all documentation
and examples I have found are a little thin on the ground.

Thanks
Mike



message news:edk%[email protected]...
Dave,

I haven't tried this, but have you thought of throwing an exception?
What happens if you do this?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dave Wright said:
Hi,

I am implementing a service that connects to an SMS Controller. We are
implementing this using an assembly in the OnStart() method of a Windows
service. This is working fine, however I cannot work out how to get the
service to stop if we cannot connect to the SMSC, if it is down or incorrect
credentials etc. Currently I am using a ServiceController class which
I
 

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