0

I have a MoveTo method which moves a cursor in my own WaveProvider class. I implemented a Phase Vocoder in this class, so when I move a cursor I need to recreate some objects with new values. There is no problem with WaveOut, but when I use DirectSound, it throws ArgumentNullException.
The problem is that, when program is in MoveTo method, and is changing some values inside object, DirectSound is inside Read method and it makes a crash. I tried to lock WaveProvider during changing position, but DirectSound still can get in Read method.
How to fix it?

apocalypse
  • 5,764
  • 9
  • 47
  • 95

1 Answers1

1

What are you locking on? You need to create a lock object inside your WaveProvider and lock on that during both your MoveTo and Read methods:

class MyWaveProvider : IWaveProvider
{
    private object lockObject = new Object();

    public void MoveTo(int pos)
    {
        lock(lockObject)
        {
            // perform the move
        }
    }


    public int Read(byte[] buffer, int offset, int count)
    {
        lock(lockObject)
        {
            // perform the read
        }
    }
}
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • After this my application freezes :( – apocalypse Sep 18 '12 at 07:31
  • are you calling MoveTo in response to an event raised within Read? You'll need to break in the debugger and look at stack traces to find what is causing your deadlock – Mark Heath Sep 18 '12 at 10:35
  • I'll will try trace it, but now I don't know anything about it. I call MoveTo from GUI thread when I do a mouse click on my WaveForm control. I really don't know why DirectSound enters Read method even when I use lock (someObject). Hmm. Need to test more. – apocalypse Sep 18 '12 at 11:44