我可以阻止垃圾收集器停止我的某些线程吗?

发布于 2024-07-08 13:43:56 字数 117 浏览 9 评论 0原文

在 Compact Framework 应用程序中是否可以阻止垃圾收集器无条件停止至少一个线程,或者至少在代码的某些部分阻止 GC 收集?

我认为它必须处理设置实时优先级,但我发现很多建议反对这样做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-07-15 13:43:56

GC 需要冻结所有线程才能检查所有对象。 如果某个线程正在运行并且正在修改/创建对象,它如何完成其​​工作?

最好不要这样做。

不过,您可以做的是在进入您不希望被中断的状态之前调用 GC.Collect() 和 GC.WaitForPendingFinalizers()。 这会给你一些时间。

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.

記憶穿過時間隧道 2024-07-15 13:43:56

非托管代码不允许访问未固定的托管对象,但它将在垃圾收集期间运行而不会阻塞。 如果您有某些例程必须在垃圾收集期间保持运行,并且它们不需要访问未固定的托管对象,则您可以在非托管代码中编写这些例程,并且 GC 不会影响它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文