From this topic Dispose()should be explicitly called for disposable items.
Dispose()
What happens when a windows service stops? Are the resources used by these objects automatically freed? Or they must be disposed of before the service stops as follows:
public void Stop() { cancellationTokenSource.Cancel(); waitForAllTasksToExit(); cancellationTokenSource.Dispose(); }
“Good practice is deliverance,” “They should be freed when the service stops,” is also what I think. But is there a specific answer with reference to the documentation?
, . Dispose , , "" Dispose, . , . ( ) :
Dispose
, , ..
, , .
.
, , , . , ( ) - , \ .
. .NET Framework. , , - - , "" . , "", - .
, IDisposable, . , , , .
IDisposable
@Evk , . .
:
, .
, IDisposable? ? . Dispose Pattern:
GC , , .
, ? ?
Finalize ( ), GC, GC .
, , IDisposable.Dispose, Finalize, SafeHandle, if Dispose, - ; .
IDisposable.Dispose
Finalize , .
Finalize
, , 2 (Finalize SafeHandle) ? : ( , ).
SafeHandle
TL;DR
( , ). ( ) Stop .
Stop
As best practice, it Dispose()should be invoked explicitly on a one-time resource. But when you stop the Windows service, it sends a control message to the entire underlying process (your .NET code) and stops or unloads everything AppDomain. Thus, all resources and memory used must be freed essentially
AppDomain