title: System out of memory exception mediaelement wpf
body: <p>I have screensaver app this app playing video and photo work normally on two hours and i get System out of memory exception how to resolve this problem?<br>
  i set the timer for 10 secodns and call SlideShow() method..</p>
  
  <pre><code>private void SlideShow()
  {
       if (animating == false)
           {
                  animating = true;
                  if (images.Count &lt;= imgIndex)
                      imgIndex = 0;
  
                  index = images.Count &gt; 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 &gt; 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);
              }
          }
  </code></pre>