private void button1_Click(object sender, EventArgs e)
        {
            aTimer = new System.Timers.Timer(3000);
            aTimer.Elapsed += new ElapsedEventHandler(button2_Click);
            aTimer.Enabled = true;
            
            for (int i = 0; i < 16; i++)
            {
                mciSendString("open new Type waveaudio Alias record_sound", "", 0, 0);
                mciSendString("record record_sound", "", 0, 0);
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            mciSendString("save record_sound c:\\Users\\jamie\\desktop\\jamie.wav", "", 0, 0);
            mciSendString("close record_sound", "", 0, 0);
            MessageBox.Show("!!");
            aTimer.Stop();
        }

I'm try to record a voice using the MCIsendstring and then set timer to save the voice after 3 seconds, but there seems to be an error. The Messagebox.Show works where i can see !! being pop up but i was not able to save the file. The code works if I press the button manually.

Please help.

Dani AI

Generated

As hinted and as discovered, this is most often a timer/threading interaction rather than a bug in the MCI commands themselves. System.Timers.Timer.Elapsed fires on a ThreadPool thread. Many WinMM/MCI operations behave differently when invoked off the UI thread or off the thread that manages the recording device. Using a dedicated thread (what fixed the original post) or marshaling the save/close calls back to a thread with a message loop will avoid the silent failures you were seeing.

A simple, low-impact approach is to marshal the timer callback to the UI thread and perform the MCI save/close there:

private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // marshal to the Form/UI thread
    this.BeginInvoke((Action)SaveRecording);
}

private void SaveRecording()
{
    // call mciSendString(...) to save/close here
}

Also add error checking around mciSendString so failures are visible. Use mciSendString return codes and mciGetErrorString to get a readable message:

[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern int mciSendString(string command, StringBuilder returnValue, int returnLength, IntPtr winHandle);

[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern bool mciGetErrorString(int err, StringBuilder text, int length);

Log the return value and the error string if nonzero. Extra tips: do not open the same device repeatedly in a tight loop (open once, record, then stop/save), consider System.Windows.Forms.Timer if all work must run on the UI thread, and for long-term robustness prefer a managed audio library such as NAudio. For timer behavior see the .NET docs for System.Timers.Timer and for marshaling see Control.BeginInvoke.

Recommended Answers

All 3 Replies

It could be a threading issue or somthing similar. What happens when you step through it and have you tried using try and catch around the affected code?

Well everything works fine unless the fact that I can't save the wave file.

solved with by using thread instead. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.