Should disposable objects be placed before the Windows service stops?

From this topic Dispose()should be explicitly called for disposable items.

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?

+8
source share
3 answers

, . Dispose , , "" Dispose, . , . ( ) :

, , .

.

, , , . , ( ) - , \ .

. .NET Framework. , , - - , "" . , "", - .

, IDisposable, . , , , .

+5

@Evk , . .

:

, .

, IDisposable? ? . Dispose Pattern:

GC , , .

, ? ?

Finalize ( ), GC, ​​GC .

:

  • , GC , .
  • , .

, , IDisposable.Dispose, Finalize, SafeHandle, if Dispose, - ; .

Finalize , .

, , 2 (Finalize SafeHandle) ? : ( , ).

TL;DR

( , ). ( ) Stop .

+2

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

0
source

All Articles