Do I need to override the OnClosing event when I'm closing a thread or would the Form_Closing event

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
 
D

David Wrighton [MS]

You can use the Closing event for this purpose.

David Wrighton
..NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Asheesh" <[email protected]>
| Subject: Do I need to override the OnClosing event when I'm closing a
thread or would the Form_Closing event would work fine?
| Date: Wed, 4 Feb 2004 14:43:22 +0530
| Lines: 87
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 210.160.205.35
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:44802
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| 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
|
|
|
 
A

Asheesh

Thanks for your reply, David. But I'd like to know as in what conditions do
we override the closing event or the Form's closing event normally suffices
in most of the cases.

Regards,
Asheesh
David Wrighton said:
You can use the Closing event for this purpose.

David Wrighton
NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Asheesh" <[email protected]>
| Subject: Do I need to override the OnClosing event when I'm closing a
thread or would the Form_Closing event would work fine?
| Date: Wed, 4 Feb 2004 14:43:22 +0530
| Lines: 87
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 210.160.205.35
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:44802
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| 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
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top