none
AccessViolationException when multithreading p/invokes of winmm.dll RRS feed

  • Question

  • Hi, I use P/invoke calls to call unmanaged WaveIn WaveOut functions from "winmm.dll" to a managed c# program. It works fine when I use them in a single thread application. But when I switch to a multi thread mode. Application work OK for a random period of time (between 1 to 3 hours)then I got this exceptions:

    AccessViolationException

    What does I miss?

    Thanks,

    Wednesday, September 26, 2012 2:19 PM

All replies

  • Can you post a code how you are using it?

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, September 26, 2012 3:16 PM
  • Hi RohitArora,

    This is a sample of how I declare P/invoke calls, I declare them in a static class as static extern


    static internal class WaveNative { private const string mmdll = "winmm.dll"; [DllImport(mmdll)] public static extern int waveOutOpen(ref IntPtr hWaveOut , int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags); [DllImport(mmdll)] public static extern int waveInOpen(ref IntPtr phwi, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);

    [DllImport(mmdll)]
        public static extern int waveInAddBuffer(IntPtr hwi, ref WaveHdr pwh, int cbwh);

    }


    from another class called "WaveIn" I invoke a method called "ThreadProc" in a separate thread, which use one of the p/invoke calls

    private void ThreadProc()
    {
    	while (!m_Finished)
    	{
    		Advance();
    		if (m_DoneProc != null && !m_Finished)
    			m_DoneProc(m_CurrentBuffer.Data, m_CurrentBuffer.Size);
    		m_CurrentBuffer.Record();
    	}
    }
    
    
    public bool Record()
    {
            lock(this)
    	{
    		m_RecordEvent.Reset();
    		m_Recording = WaveNative.WaveInAddBuffer(m_WaveIn, ref m_Header, Marshal.SizeOf(m_Header)) == WaveNative.MMSYSERR_NOERROR;
    		return m_Recording;
    	}
    }

    WaveIn class is instantiated from class called "TR", the later is instantiated multiple times from the the MainForm.

    So you can say that, more than one "ThreadProc" can run in separate threads and use the same static p/invoke call "waveInAddBuffer".

    Is winmm.dll support multithreading, is it thread safe?

    Wednesday, September 26, 2012 5:29 PM
  • It is hard to say.

    But I think all API is thread safe.

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Friday, September 28, 2012 10:24 AM