// Releases buffers. private void ReleaseBuffer(object state) { lock (lockObject) { IntPtr headerPtr = (IntPtr)state; // Unprepare the buffer. int result = midiOutUnprepareHeader(Handle, headerPtr, SizeOfMidiHeader); if (result != MidiDeviceException.MMSYSERR_NOERROR) { Exception ex = new OutputDeviceException(result); OnError(new ErrorEventArgs(ex)); } // Release the buffer resources. headerBuilder.Destroy(headerPtr); bufferCount--; Monitor.Pulse(lockObject); Debug.Assert(bufferCount >= 0); } }