ServiceBase.Dispose(Boolean) Method

Definition

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

protected override void Dispose (bool disposing);

Parameters

disposing
Boolean

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

Remarks

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.

Note

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.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also