gpt4 book ai didi

c# - 如何处理 System.Windows.Media.MediaPlayer

转载 作者:行者123 更新时间:2023-11-30 12:25:51 25 4
gpt4 key购买 nike

问题很简单,可以总结为:

如何让这个 while 循环退出?

System.Windows.Media.MediaPlayer player = new System.Windows.Media.MediaPlayer();
WeakReference test = new WeakReference(player);

player.Close();
player = null;

while (test.IsAlive && test.Target != null)
{
System.GC.Collect();
}

我已经搜索了文档并没有找到处理这个对象的方法,while 循环永远不会退出。

最佳答案

令我惊讶的是,C# 文档和 Java 文档都没有如此清楚地描述它:

如果没有内存压力,则不会收集 WeakReference。

MSDN文章可能有帮助:

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object.

许可不是必需的。它可以这样做,但不需要。

WikipediaVariations 下得到它(对于 Java,但 Java 和 C# 非常相似):

[...] the GC may decide not to do so if it believes the JVM can spare the memory (e.g. the JVM has lots of unused heap space)

关于c# - 如何处理 System.Windows.Media.MediaPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com