using Thread.Start() on every click or using a threadpool

A

Asheesh

Hi All,

I'm using 2 listview controls in a form. Both listview controls contain
items. The items in both listview controls have 1-1 mapping with each other.
E.g. Item 1 is listView control 1 is related to item 2 in listView control
2.
Now I've associated a single click with the Item_Activate event of the
listView control. So, that when Item 1 of listView control 1 is clicked, the
corresponding item 1 in the listView control is highlighted.

For this I'm using listView.EnsureVisible method in source listview control
to highlight the corresponding item in the target listview control.

E.g
private void lvSource_ItemActivate(object sender, System.EventArgs e)
{
int i=lvSource.SelectedIndices[0];
lvTarget.Items.Selected = true;
lvTarget.EnsureVisible(i);
//Plays the audio mp3 for the clicked item.
Player.PlayAudioFile(Convert.ToInt32(lvSource.Items.SubItems[1].Text),"So
urce");
}

All this works fine.. Now when a particular item is clicked on listview
control, I also play that particular item audio file, which is MP3 by using
an audio engine.

For this I PInvoke the particular function and then use it's PlayAudioFile
function listed above to play the audio file.
All this is fine, but when I play the audio file, the audio file plays
first and then the corresponding item in the target listview is highlighted.

Since it occurs in the main thread, there is a delay since first the audio
files playbacks and then the item is highlighted.
For this I'm thinking of playing in a separate thread, but I'm confused as
to should I start a new thread everytime, when the user clicks the item and
execute the playAudioFile method or use the ThreadPool class to execute my
method by virtue of threads being maintained by it.

Are there any performance benefits associated with ThreadPool class?
Moreover, I have a fear, since ThreadPool class can generate multiple
threads for execution, there might be a case, where a user might click on a
particular item while the audio playback of first item is still in progress.
This way, a memory exception might occur, since the second thread would try
to create the audio object in my dll and try to play the audio file, while a
previous audio file is still in progress.

Is there a way to avoid this possible conflict by making use of a ThreadPool
or even spawning a new thread?

Please help,
Any help would be greatly appreciated.
Regards,
Asheesh
 
A

Asheesh

Hi,

I tried to use ThreadPool but when I play the audio file I get this error
after setting the SetLastError to true, and then obtaining the Error code
using Marshal.GetLastWin32Error() as
-2147483643.

After playing another audio file, I get error code as 120.
2 different error codes.
After a while, I get a NativeException error occurred, and I have to Quit
the application in order to proceed.

Here's my piece of code.

I execute this method in my lvSource _ItemActivate method
if(Global.PlayCues==true)
ThreadPool.QueueUserWorkItem(CuePlayback,(object)true);

My CuePlayBack function

private void PlayCue(object state)
{
if(Convert.ToBoolean(state)==true)
Player.PlayAudioCues("right.mp3_");
else
Player.PlayAudioCues("wrong.mp3_");
}

But when I play these audio files in the single UI thread, all plays
normally, but albeit it delays the entire highlighting process of the item
in the target listview control.

Any ideas, Please help.

regards,
Asheesh
Asheesh said:
Hi All,

I'm using 2 listview controls in a form. Both listview controls contain
items. The items in both listview controls have 1-1 mapping with each other.
E.g. Item 1 is listView control 1 is related to item 2 in listView control
2.
Now I've associated a single click with the Item_Activate event of the
listView control. So, that when Item 1 of listView control 1 is clicked, the
corresponding item 1 in the listView control is highlighted.

For this I'm using listView.EnsureVisible method in source listview control
to highlight the corresponding item in the target listview control.

E.g
private void lvSource_ItemActivate(object sender, System.EventArgs e)
{
int i=lvSource.SelectedIndices[0];
lvTarget.Items.Selected = true;
lvTarget.EnsureVisible(i);
//Plays the audio mp3 for the clicked item.
Player.PlayAudioFile(Convert.ToInt32(lvSource.Items.SubItems[1].Text),"So
urce");
}

All this works fine.. Now when a particular item is clicked on listview
control, I also play that particular item audio file, which is MP3 by using
an audio engine.

For this I PInvoke the particular function and then use it's PlayAudioFile
function listed above to play the audio file.
All this is fine, but when I play the audio file, the audio file plays
first and then the corresponding item in the target listview is highlighted.

Since it occurs in the main thread, there is a delay since first the audio
files playbacks and then the item is highlighted.
For this I'm thinking of playing in a separate thread, but I'm confused as
to should I start a new thread everytime, when the user clicks the item and
execute the playAudioFile method or use the ThreadPool class to execute my
method by virtue of threads being maintained by it.

Are there any performance benefits associated with ThreadPool class?
Moreover, I have a fear, since ThreadPool class can generate multiple
threads for execution, there might be a case, where a user might click on a
particular item while the audio playback of first item is still in progress.
This way, a memory exception might occur, since the second thread would try
to create the audio object in my dll and try to play the audio file, while a
previous audio file is still in progress.

Is there a way to avoid this possible conflict by making use of a ThreadPool
or even spawning a new thread?

Please help,
Any help would be greatly appreciated.
Regards,
Asheesh
 
J

Jeffrey Huntsman

Why don't you highlight the item first, then play the audio? You can also
change the cursor to an hourglass before your call to PlayAudio, then change
it back to default cursor immediately after. I also don't know which audio
engine you are using, but Windows API allows you to invoke the PlaySound
function asynchronously. You may want to see if you can do that with your
audio engine, then you will not have the pause because the thread will not
be blocked by the PlayAudio call.

Jeff


Asheesh said:
Hi All,

I'm using 2 listview controls in a form. Both listview controls contain
items. The items in both listview controls have 1-1 mapping with each other.
E.g. Item 1 is listView control 1 is related to item 2 in listView control
2.
Now I've associated a single click with the Item_Activate event of the
listView control. So, that when Item 1 of listView control 1 is clicked, the
corresponding item 1 in the listView control is highlighted.

For this I'm using listView.EnsureVisible method in source listview control
to highlight the corresponding item in the target listview control.

E.g
private void lvSource_ItemActivate(object sender, System.EventArgs e)
{
int i=lvSource.SelectedIndices[0];
lvTarget.Items.Selected = true;
lvTarget.EnsureVisible(i);
//Plays the audio mp3 for the clicked item.
Player.PlayAudioFile(Convert.ToInt32(lvSource.Items.SubItems[1].Text),"So
urce");
}

All this works fine.. Now when a particular item is clicked on listview
control, I also play that particular item audio file, which is MP3 by using
an audio engine.

For this I PInvoke the particular function and then use it's PlayAudioFile
function listed above to play the audio file.
All this is fine, but when I play the audio file, the audio file plays
first and then the corresponding item in the target listview is highlighted.

Since it occurs in the main thread, there is a delay since first the audio
files playbacks and then the item is highlighted.
For this I'm thinking of playing in a separate thread, but I'm confused as
to should I start a new thread everytime, when the user clicks the item and
execute the playAudioFile method or use the ThreadPool class to execute my
method by virtue of threads being maintained by it.

Are there any performance benefits associated with ThreadPool class?
Moreover, I have a fear, since ThreadPool class can generate multiple
threads for execution, there might be a case, where a user might click on a
particular item while the audio playback of first item is still in progress.
This way, a memory exception might occur, since the second thread would try
to create the audio object in my dll and try to play the audio file, while a
previous audio file is still in progress.

Is there a way to avoid this possible conflict by making use of a ThreadPool
or even spawning a new thread?

Please help,
Any help would be greatly appreciated.
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