Uninstalling a NT Service programmatically

  • Thread starter Thread starter Ricky K. Rasmussen
  • Start date Start date
R

Ricky K. Rasmussen

Hi NG,

I'm writing a management tool for our software, that among other should be
able to install and uninstall some of our NT services.

The problem is that when I uninstall the services (using
ManagedInstallerClass.InstallHelper with the "/u" parameter) the services
are uninstalled, but they are still running until I close my management
application. This is a problem since I want to remove the service assemblies
as a part of the uninstallation.

I read the Knowledge Base-article 287516
(http://support.microsoft.com/default.aspx?scid=kb;en-us;287516), which
seems to be addressing my problem. It claims that all handles to the
services needs to be closed before the service can be uninstalled. Thats
fair enough, but eventhough all my ServiceControllers are Close()'d and
Dispose()'d the problem persists.

Is there a way to release all handles to a service allowing it to be
uninstalled at once?
Or is there another way to do this?

Thanks for your time,
Ricky
 
Stopping a service is an asynchronous process, you might use the
WaitForStatus to wait until the service is actually stopped.

Regards,

Peter
 
Peter,
Thanks for the reply, but it really doesn't answer my question.

I do use the WaitForStatus when stopping my Services and the services do get
stopped.

The problem is that I can't uninstall the services and then delete the
service assembles as a nice cleanup. The assemblies are still executing and
not uninstalled for real before I exit my application.

Thanks,
Ricky
 
If you stop the service, and the physical process remains
in the process viewer, then your service isn't cleaning
itself up properly.

I ran into this a while back. Turned out that I had a
thread that was in limbo due to a blocking socket call.

Services are especially unforgiving of resource leaks, as
their startup/shutdown is controlled by the SCM. You need
to be meticulous about thread management and cleanup.
Don't rely on the GC for anything.
 
Thanks for your time Steve,
but as I pointed out, I had no problems stopping the services, my question
was concerning the uninstallation of these.

And I found a solution - or at least a way to solve this issue:
Instead of calling the ManagedInstallerClass.InstallHelper directly to
uninstall the services, I make the call through an external executable. I'm
not sure why this works but I guess it has something to do with the service
handles being released when the external executable exits.

- Ricky
 
Back
Top