Export (0) Print
Expand All

ServiceBase.Dispose Method (Boolean)

Disposes of the resources (other than memory) used by the ServiceBase.

Namespace:  System.ServiceProcess
Assembly:  System.ServiceProcess (in System.ServiceProcess.dll)

protected override void Dispose(
	bool disposing


Type: System.Boolean

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Call Dispose in your derived class (and through it, in the ServiceBase class) when you are finished using the derived class. The Dispose method leaves the derived class in an unusable state. After calling Dispose, you must release all references to the derived class and ServiceBase so the memory they were occupying can be reclaimed by garbage collection.


Always call Dispose before you release your last reference to the class derived from ServiceBase. Otherwise, the resources ServiceBase and the derived class are using will not be freed until garbage collection calls the objects' destructors.

OnStop is often implemented to process the code in OnPause, combined with a call to Dispose. If you choose to do this in your derived class, it is customary to implement OnStart to allocate whatever Dispose released.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft