Domanda

When I launch my application in the visual studio debugger, it "stops working" with AppCrash:

  Fault Module Name:    KERNELBASE.dll
  Exception Code:   e0455858
  Exception Offset: 0000c41f

I've narrowed it down to my SoundPlayer which just inherits MediaPlayer and adds looping and playing state. When I comment it out the application closes down normally, I tried adding dispose to see if the sound needed to stop before the application closes, but that didn't fix it.

It should be noted that the application only crashes in the Visual Studio debugger, if I just run the executable it closes fine.

using System;

namespace GameEngine
{
    public class SoundPlayer : System.Windows.Media.MediaPlayer, IDisposable
    {
        private bool Loop = false;
        public bool Playing { private set; get; }

        public SoundPlayer(string url)
        {
            this.Open(new System.Uri(System.IO.Directory.GetCurrentDirectory() + "/" + url));
            this.MediaEnded += (sender, e) => 
            {
                this.Playing = false;
                if (this.Loop)
                {
                    this.ResetTime();
                }
            };
        }

        public void PlayOnce()
        {
            this.StartPlaying();
        }

        public void PlayLooping()
        {
            this.Loop = true;
            this.StartPlaying();
        }

        public void StopPlaying()
        {
            this.Stop();
            this.Playing = false;
        }

        public void StopLooping()
        {
            this.Loop = false;
        }

        private void StartPlaying()
        {
            if (!this.Playing)
            {
                this.Playing = true;
                this.ResetTime();
                this.Play();
            }
        }

        private void ResetTime()
        {
            this.Position = new TimeSpan(0);
        }

        public void Dispose()
        {
            this.Stop();
            this.Close();
        }
    }
}
È stato utile?

Soluzione

Fixed with:

public void Dispose()
{
   this.Stop();
   this.Close();
   this.Dispatcher.InvokeShutdown();
}

Added this.Dispatcher.InvokeShutdown(); to the Dispose method.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top