.net - How to release a managed resource which will not be garbage collected if the caller forget to use Dispose()? -


In my code, I have a class that creates a new thread. This new thread has a dispatcher, so thread will not end until I call

  Dispatcher.CurrentDispatcher.InvokeShutdown ();  

Threads can not be referenced outside of my class. So I was thinking: to ensure that my thread is over when my object garbage is collected?

An answer is to use IDisposable, but if a person forgets to call the displacement, the thread will never stop, not a solution.

Use one more reaction + Destination is to be settled, but I have heard that we should use only the destroyer for release unmanaged resources. I am at a dead end.

What is the best solution?

One option is to use only for the final debug build. Do not just stop this "well" thread: Log it in a very important way, it should help you to isolate places where you have forgotten to fix the object properly.

However, this is not easy and it is undesirable to separate the debug and release code.

Will your dispatcher usually receive many incidents? Can you file at least one alert if it has not received any event for a long time?

A fairly bad option, Dispatchers must create a weak reference for disposable objects. It can periodically check that the garbage has been collected from the object (using WeakReference.IsAlive ) and if it is turned off, then it is not very pleasant. (And a warning should be logged).


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -