系统内存不足异常mediaelement wpf

时间:2014-10-21 07:39:18

标签: c# wpf

我有屏幕保护程序应用这个应用播放视频和照片正常工作两小时我得到系统内存不足如何解决这个问题?
我将计时器设置为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);
            }
        }

1 个答案:

答案 0 :(得分:0)

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

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

    myMediaElement1.MediaEnded -= myMediaElement1_MediaEnded;

一旦Begin Animation完成,然后启动MediaElement1的BeginAnimation

,您可以为所有MediaElement属性赋值null
     myMediaElement.Source=null;
     myMediaElement.UnloadedBehavior = null;  (if possible)
      .
      .

Link进一步阐述了内存泄漏