问题
Is it possible in a Compact Framework application to prevent the garbage collector from unconditionally stopping at least one of the threads, or to block GC collects at least in some portions of the code?
I think it has to deal with setting real time priorities, but I found a lot of advice against doing it.
回答1:
The GC needs to freeze all threads in order to inspect all objects. How could it do its job, if some thread is running and is modifying/creating an object?
Better don't do it.
What you can do thogh, is to invoke GC.Collect() and GC.WaitForPendingFinalizers() before you enter in a state where you do not want to be interrupted. This will give you some time.
回答2:
Unmanaged code is not allowed to access unpinned managed objects, but it will run without blocking during garbage collection. If you have certain routines that must keep running during garbage-collection, and they don't require access to unpinned managed objects, you could write those routines in unmanaged code and the GC wouldn't affect them.
来源:https://stackoverflow.com/questions/282327/can-i-prevent-garbage-collector-from-stopping-some-of-my-threads