A
Asheesh
Hi All,
I'd like some clarification on this point. In my application I'm playing
audio files for different phrases listed in my listview control using a 3rd
party audio library. In one of the features I'm providing the user an
AutoPlay feature in which all the audio for every phrase in the listview
control is played one by one. I also need to highlight for which phrase the
audio is currently being played.
The audio playing part, and subsequent highlighting of phrase is being done
in the separate thread.
I do 2 things in order to check when the Playback is interrupted in between
or when the user closes the form.
I set a bool variable to false before the new thread is created. I set this
bool variable to true when the new thread is created.
When the user wants to stop the playback, I simply set this bool to false to
indicate that the running thread be stopped.
I've noticed that this is the common technique used in MSDN and gotdotnet
samples. However, one thing baffles me or leaves me confused... In both
Gotdotnet samples, they have overriden Form's OnClosing event and then
tested whether the thread is no longer running or not.
Do I also have to do the same or can I use Form_Closing event? In the
OnClosing event I just check for the bool variable, if it's false, then I
close the form else I the cancel event to true.
this is the code I'm using.
Starts the thread.
private void pctPrePlay_Click(object sender, EventArgs e)
{
AutoPlay=true;
TotListViewItem=lvTranslations.Items.Count;
CurrListViewItemPos=lvTranslations.SelectedIndices[0];
//ThreadPool.QueueUserWorkItem(new WaitCallback(AutoPlayPhrases));
AutoThread=new Thread(new ThreadStart(AutoPlayPhrases));
AutoThread.Start();
}
Function executed on different thread.
private void AutoPlayPhrases()
{
for (int i=CurrListViewItemPos;i<TotListViewItem && AutoPlay==true;i++)
{
ListViewId=i;
lvTranslations.Invoke(new EventHandler(HighlightPhrases));
lblTranslation.Invoke(new EventHandler(TranslateText));
Player.PlayAudioFile(ItemId,"Target");
}
AutoPlay=false;
}
Closing Function...
private void FrmPhrasebookReview_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(AutoPlay==true)
e.Cancel=true;
else
{
e.Cancel=false;
Global.ShowHomeForm();
}
this.AutoPlay=false;
}
Do I need to override OnClosing function here since the above snippet works
fine.
Also I was thinking of using the systems's ThreadPool(that would save me
from creating and destroying threads). But I'd have to do the bool variable
check in case of ThreadPool as well?
I'd appreciate if some one could shed some light on this, and point out any
deficiencies in my code.
Thanks in advance.
Regards,
Asheesh
I'd like some clarification on this point. In my application I'm playing
audio files for different phrases listed in my listview control using a 3rd
party audio library. In one of the features I'm providing the user an
AutoPlay feature in which all the audio for every phrase in the listview
control is played one by one. I also need to highlight for which phrase the
audio is currently being played.
The audio playing part, and subsequent highlighting of phrase is being done
in the separate thread.
I do 2 things in order to check when the Playback is interrupted in between
or when the user closes the form.
I set a bool variable to false before the new thread is created. I set this
bool variable to true when the new thread is created.
When the user wants to stop the playback, I simply set this bool to false to
indicate that the running thread be stopped.
I've noticed that this is the common technique used in MSDN and gotdotnet
samples. However, one thing baffles me or leaves me confused... In both
Gotdotnet samples, they have overriden Form's OnClosing event and then
tested whether the thread is no longer running or not.
Do I also have to do the same or can I use Form_Closing event? In the
OnClosing event I just check for the bool variable, if it's false, then I
close the form else I the cancel event to true.
this is the code I'm using.
Starts the thread.
private void pctPrePlay_Click(object sender, EventArgs e)
{
AutoPlay=true;
TotListViewItem=lvTranslations.Items.Count;
CurrListViewItemPos=lvTranslations.SelectedIndices[0];
//ThreadPool.QueueUserWorkItem(new WaitCallback(AutoPlayPhrases));
AutoThread=new Thread(new ThreadStart(AutoPlayPhrases));
AutoThread.Start();
}
Function executed on different thread.
private void AutoPlayPhrases()
{
for (int i=CurrListViewItemPos;i<TotListViewItem && AutoPlay==true;i++)
{
ListViewId=i;
lvTranslations.Invoke(new EventHandler(HighlightPhrases));
lblTranslation.Invoke(new EventHandler(TranslateText));
Player.PlayAudioFile(ItemId,"Target");
}
AutoPlay=false;
}
Closing Function...
private void FrmPhrasebookReview_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(AutoPlay==true)
e.Cancel=true;
else
{
e.Cancel=false;
Global.ShowHomeForm();
}
this.AutoPlay=false;
}
Do I need to override OnClosing function here since the above snippet works
fine.
Also I was thinking of using the systems's ThreadPool(that would save me
from creating and destroying threads). But I'd have to do the bool variable
check in case of ThreadPool as well?
I'd appreciate if some one could shed some light on this, and point out any
deficiencies in my code.
Thanks in advance.
Regards,
Asheesh