gpt4 book ai didi

c# - 系统内存不足异常mediaelement wpf

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:38 24 4
gpt4 key购买 nike

我有屏幕保护应用程序,这个应用程序播放视频和照片正常工作两个小时,我得到系统内存不足异常如何解决这个问题?
我将计时器设置为 10 秒并调用 SlideShow() 方法..

private void SlideShow()
{
if (animating == false)
{
animating = true;
if (images.Count <= imgIndex)
imgIndex = 0;

index = images.Count > imgIndex ? imgIndex : 0;

imgIndex++;
fadeOUT.From = 1;
fadeOUT.To = 0;
fadeOUT.Duration = TimeSpan.FromSeconds(CrossFadeTime);
fadeOUT.Completed += new EventHandler(fadeIN_Completed);

fadeIN.From = 0;
fadeIN.To = 1;
fadeIN.Duration = TimeSpan.FromSeconds(CrossFadeTime);
fadeIN.Completed += new EventHandler(fadeIN_Completed);
if (images[index].Contains(".wmv") || images[index].Contains(".mkv") || images[index].Contains(".mp4"))
myMediaElement.Visibility = System.Windows.Visibility.Hidden;
else
{
if (myMediaElement.Visibility == System.Windows.Visibility.Hidden)
myMediaElement.Visibility = System.Windows.Visibility.Visible;
}

myMediaElement.UnloadedBehavior = MediaState.Manual;
myMediaElement.Stop();
myMediaElement.Source = null;
myMediaElement.Close();
myMediaElement.Source = new Uri(images[index], UriKind.RelativeOrAbsolute);
myMediaElement.Stretch = Stretch.Uniform;
myMediaElement.Volume = 0;

myMediaElement1.UnloadedBehavior = MediaState.Manual;
myMediaElement1.Stop();
myMediaElement1.Source = null;
myMediaElement1.Close();
System.Threading.Thread.Sleep(900);
myMediaElement1.Source = new Uri(images[images.Count > index + 1 ? index + 1 : 0], UriKind.RelativeOrAbsolute);
myMediaElement1.Stretch = Stretch.Uniform;
myMediaElement1.MediaOpened += myMediaElement1_MediaOpened;
myMediaElement1.MediaEnded += myMediaElement1_MediaEnded;
myMediaElement.BeginAnimation(Image.OpacityProperty, fadeOUT, HandoffBehavior.SnapshotAndReplace);
myMediaElement1.BeginAnimation(Image.OpacityProperty, fadeIN, HandoffBehavior.SnapshotAndReplace);
}
}

最佳答案

您必须尽可能减少应用程序的内存消耗,直到异常消失。

您可以通过在子窗口关闭时取消注册事件来减少内存消耗。例如:

    myMediaElement1.MediaEnded -= myMediaElement1_MediaEnded;

Begin Animation 完成后,您可以将 null 分配给所有 MediaElement 属性,然后启动 MediaElement1 的 BeginAnimation

     myMediaElement.Source=null;
myMediaElement.UnloadedBehavior = null; (if possible)
.
.

Link进一步阐述内存泄漏

关于c# - 系统内存不足异常mediaelement wpf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481296/

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