tags:

views:

64

answers:

3

Problem: The wav files plays, but GUI does not respond at all. The lbl_PhonoString.Text is not updated every loop. User cannot pause or cancel until all the files are played completely.

I know I have to use a seaparate thread to play the Wav file. I am not at all familiar with threading. Can someone suggest what I should do here?

My Code:

//====================================================
bool bPause = false, bCancel = false;

private void btn_DicStart_Click(object sender,EventArgs e)
{
    PlayWave("StartProgram.WAV");
    for(int i=0;i<10;i++)
    {
        lbl_PhonoString.Text=i.ToString();
        PlayWave("NextSound.WAV");
        PlayWave("Clue"+i.ToString()+".WAV");

        //Check if user pressed pause/cancel
        while(bPause);

        if(bCancel)
            break;
    }
}

private void btn_Pause_Click(object sender,EventArgs e)
{
    bPause=!bPause;
    if(!bPause)
        btn_Pause.Text ="PAUSE";
    else
        btn_Pause.Text ="CONTINUE";
}

private void btn_Cancel_Click(object sender,EventArgs e)
{
    bCancel=true;
}

//Play the Wave File 
private void PlayWave(string WaveFile)
{
    System.Media.SoundPlayer myPlayer=new System.Media.SoundPlayer();
    myPlayer.SoundLocation=WaveFile;
    myPlayer.PlaySync();
}
//====================================================
+1  A: 

Look here essentially the same question and the accepted answer is your answer. In a nutshell, yes, you should be doing all long running processes in a seperate thread or they will block the UI, or use BackgroundWorker of course.

Serapth
A: 

Can someone guide me on how to use multi threading?

A: 

[DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]

PlaySound("WavFileName.wav", new System.IntPtr(), EnumPlaySoundFlags.SND_ASYNC); Doe not work. Only the last file in the loop will play. All other files are skipped