0

I have a question about using external c++ library (irrKlang.dll) which is an audio playback engine. Now, the problem is that when I get a SoundStopped event out of it, and do an action in the main form, all kinds of stack related errors arise. Let me show the code:

namespace WindowsFormsApplication4

{
    public class IsoundFinished : ISoundStopEventReceiver
    {
        public delegate void OnSoundStoppedEventHandler(object source, EventArgs e);
        public event OnSoundStoppedEventHandler IStopped;

        public void OnSoundStopped(ISound iSound, StopEventCause reason, object userData)
        {
            if (reason.ToString() == "SoundFinishedPlaying")
                IStopped?.Invoke(this, EventArgs.Empty);
        }
    }
}

That is an extended class for me to do custom actions (for example - if sound finished, raise the event...) I am creating an instance of it, for the event action to get exposed in my main Form1 class:

IsoundFinished iStopReceiver = new IsoundFinished();

Now in my main form, I have this line in my Form1() method, just under my InitializeComponent():

iStopReceiver.IStopped += new soundFinished.OnSoundStoppedEventHandler(OnStopped);

It's for subscribing to the event handler. And finally - my OnStopped() method which is supposed to do stuff when the song ends it's playback - it's on the same Form1:

 private void OnStopped(object sender, EventArgs e)
        {            
            if (InvokeRequired)
            {
                Invoke(new Action<object, EventArgs>(OnStopped), sender, e);
                return;
            }            

            btnStop1.PerformClick();
        }

My Stop1 button method is (for those who work with the IrrKlang) ISound.Stop(); and few more lines of code, dealing with the display of playlist and so on. Although I have invoked it from the main UI thread - which should provide me with some degree of thread misalignment protection, all kinds of errors appear, mostly

Cannot evaluate expression because a native frame is on the top of the call stack.

Of course, if I do it without event handler, ISound.Stop(); drops the sound from the engine, like it should. I know something wrong is happening with the threads, but I can't figure out what's going on. If someone would give me few tips, I'd appreciate that a lot.

1 Answers1

0

Well it seems I've solved it myself ! It's all about understanding how the threads are working in Visual C#. The problem was this : I was actually PAUSING the background thread where my audioengine was triggering the event - so 'till I performed an action after INVOKE in the main UI thread, background thread was paused along with the whole irrKlang engine. It was unable to purge itself properly, so it's call stack got clogged!

Using BEGININVOKE solved the problem, as it doesn't PAUSE the background task. It lets it run instead. Diagram on this answer gave me much needed piece of info I was looking for. Maybe someone will need this answer too, glad I helped myself :P

private void OnStopped(object sender, EventArgs e)
        {            
            if (InvokeRequired)
            {
                BeginInvoke(new Action<object, EventArgs>(OnStopped), sender, e);
                return;
            }            

            btnStop1.PerformClick();                
        }
Community
  • 1
  • 1