Is the command to check if the bell is running or stopped ?
-
I have the following C # statement:
...
using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
{
player.Play();//What should I write here?
while (If the bell is running, wait here)
{
if (player.Stop()==true)
{
this.progressBarControl1.Visible = false; //false = không cho phép hiện
}
}
}
...I want to check if the ringer is running or stopping, if it is running then wait until it stops before running this.progressBarControl1.Visible = false.
-
I have the following C # statement:
...
using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
{
player.Play();//What should I write here?
while (If the bell is running, wait here)
{
if (player.Stop()==true)
{
this.progressBarControl1.Visible = false; //false = không cho phép hiện
}
}
}
...I want to check if the ringer is running or stopping, if it is running then wait until it stops before running this.progressBarControl1.Visible = false.
If you need to know when the sound has finished playing, you'll have to use the
PlaySync
method[^]. However, if you do that from the UI thread, your application will stop responding until the sound finishes. You'll probably want to play the sound on a background thread to avoid this problem. The simplest option would probably be to use aBackgroundWorker
instance[^] - put theSoundPlayer
code in theDoWork
event handler, and the code to hide the progress bar in theRunWorkerCompleted
event handler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
If you need to know when the sound has finished playing, you'll have to use the
PlaySync
method[^]. However, if you do that from the UI thread, your application will stop responding until the sound finishes. You'll probably want to play the sound on a background thread to avoid this problem. The simplest option would probably be to use aBackgroundWorker
instance[^] - put theSoundPlayer
code in theDoWork
event handler, and the code to hide the progress bar in theRunWorkerCompleted
event handler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
use PlaySync() runs fine, but using backgroundWorker1 is difficult to use and the computer freezes
-
use PlaySync() runs fine, but using backgroundWorker1 is difficult to use and the computer freezes
Member 2458467 wrote:
backgroundWorker1 is difficult to use and the computer freezes
Then you're using it wrong.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Member 2458467 wrote:
backgroundWorker1 is difficult to use and the computer freezes
Then you're using it wrong.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I do not know how to use backgroundWorker in my code so the device crashes, I troubleshoot I see Debug.Print("Status: PLAY") and Debug.Print("Status: STOP") appear only once, You see my code, did I write anything wrong ?
bool _isStopped = true;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;lblStatusPlaySound.ForeColor = Color.Violet; lblStatusPlaySound.Text = "Status: PLAY/STOP";
}
private void btnPlaySound_Click(object sender, EventArgs e)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.Play(); } while (\_isStopped == true) { if (backgroundWorker1.IsBusy != true) { \_isStopped = true; } else // { \_isStopped = false; } lblStatusPlaySound.ForeColor = Color.Green; lblStatusPlaySound.Text = "Status: PLAY"; lblStatusPlaySound.Refresh(); // Debug.Print("Status: PLAY"); } lblStatusPlaySound.ForeColor = Color.Red; lblStatusPlaySound.Text = "Status: STOP"; lblStatusPlaySound.Refresh(); Debug.Print("Status: STOP");
}
-
I do not know how to use backgroundWorker in my code so the device crashes, I troubleshoot I see Debug.Print("Status: PLAY") and Debug.Print("Status: STOP") appear only once, You see my code, did I write anything wrong ?
bool _isStopped = true;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;lblStatusPlaySound.ForeColor = Color.Violet; lblStatusPlaySound.Text = "Status: PLAY/STOP";
}
private void btnPlaySound_Click(object sender, EventArgs e)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.Play(); } while (\_isStopped == true) { if (backgroundWorker1.IsBusy != true) { \_isStopped = true; } else // { \_isStopped = false; } lblStatusPlaySound.ForeColor = Color.Green; lblStatusPlaySound.Text = "Status: PLAY"; lblStatusPlaySound.Refresh(); // Debug.Print("Status: PLAY"); } lblStatusPlaySound.ForeColor = Color.Red; lblStatusPlaySound.Text = "Status: STOP"; lblStatusPlaySound.Refresh(); Debug.Print("Status: STOP");
}
Read my previous message again: put the
SoundPlayer
code in theDoWork
event handler, and the code to hide the progress bar in theRunWorkerCompleted
event handler. You've put all of the code in the button'sClick
event handler instead. And as I said, you'll need to use thePlaySync
method if you want to wait for the sound to finish playing. It should look something like:public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;lblStatusPlaySound.ForeColor = Color.Violet;
lblStatusPlaySound.Text = "Status: PLAY/STOP";
}private void btnPlaySound_Click(object sender, EventArgs e)
{
btnPlaySound.Enabled = false;
lblStatusPlaySound.ForeColor = Color.Green;
lblStatusPlaySound.Text = "Status: PLAY";
backgroundWorker1.RunWorkerAsync();
}private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Debug.Print("Status: PLAY");using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.PlaySync(); }
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblStatusPlaySound.ForeColor = Color.Red;
lblStatusPlaySound.Text = "Status: STOP";
lblStatusPlaySound.Refresh();
btnPlaySound.Enabled = true;
Debug.Print("Status: STOP");
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Read my previous message again: put the
SoundPlayer
code in theDoWork
event handler, and the code to hide the progress bar in theRunWorkerCompleted
event handler. You've put all of the code in the button'sClick
event handler instead. And as I said, you'll need to use thePlaySync
method if you want to wait for the sound to finish playing. It should look something like:public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;lblStatusPlaySound.ForeColor = Color.Violet;
lblStatusPlaySound.Text = "Status: PLAY/STOP";
}private void btnPlaySound_Click(object sender, EventArgs e)
{
btnPlaySound.Enabled = false;
lblStatusPlaySound.ForeColor = Color.Green;
lblStatusPlaySound.Text = "Status: PLAY";
backgroundWorker1.RunWorkerAsync();
}private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Debug.Print("Status: PLAY");using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.PlaySync(); }
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lblStatusPlaySound.ForeColor = Color.Red;
lblStatusPlaySound.Text = "Status: STOP";
lblStatusPlaySound.Refresh();
btnPlaySound.Enabled = true;
Debug.Print("Status: STOP");
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Hi Richard MacCutchan! Follow me, the above code can be edited without using RunWorkerCompleted, the program still returns the same result because the PlaySync() method is a sequential command, simple the code will be easier to understand, you see the code below. RunWorkerCompleted event in other cases eg Play() instead PlaySync() method to catch "PLAY/STOP" status for example, do you think ? that is my opinion.
private void btnPlaySound_Click(object sender, EventArgs e)
{
lblStatusPlaySound.ForeColor = Color.Green;
lblStatusPlaySound.Text = "Status: PLAY";
Application.DoEvents();
Debug.Print("Status: PLAY");using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.PlaySync(); } lblStatusPlaySound.ForeColor = Color.Red; lblStatusPlaySound.Text = "Status: STOP"; Application.DoEvents(); Debug.Print("Status: STOP");
}
-
Hi Richard MacCutchan! Follow me, the above code can be edited without using RunWorkerCompleted, the program still returns the same result because the PlaySync() method is a sequential command, simple the code will be easier to understand, you see the code below. RunWorkerCompleted event in other cases eg Play() instead PlaySync() method to catch "PLAY/STOP" status for example, do you think ? that is my opinion.
private void btnPlaySound_Click(object sender, EventArgs e)
{
lblStatusPlaySound.ForeColor = Color.Green;
lblStatusPlaySound.Text = "Status: PLAY";
Application.DoEvents();
Debug.Print("Status: PLAY");using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring)) { player.PlaySync(); } lblStatusPlaySound.ForeColor = Color.Red; lblStatusPlaySound.Text = "Status: STOP"; Application.DoEvents(); Debug.Print("Status: STOP");
}
Member 2458467 wrote:
Hi Richard MacCutchan!
Wrong Richard.
Member 2458467 wrote:
because the PlaySync() method is a sequential command
And that's the problem. The thread that calls
PlaySync
is blocked until the sound finishes playing. If you call it from the UI thread, your entire application will freeze, and you'll get the "Application is not responding" message if you try to interact with it. It might be OK for a very short sound, so long as you don't expect the UI to update whilst it's playing. But for anything longer than half a second, you need to play the sound from a background thread. Which is where theBackgroundWorker
comes in. SprinklingApplication.DoEvents()
calls throughout your code is a hack, and a sign of code which needs to be changed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Member 2458467 wrote:
Hi Richard MacCutchan!
Wrong Richard.
Member 2458467 wrote:
because the PlaySync() method is a sequential command
And that's the problem. The thread that calls
PlaySync
is blocked until the sound finishes playing. If you call it from the UI thread, your entire application will freeze, and you'll get the "Application is not responding" message if you try to interact with it. It might be OK for a very short sound, so long as you don't expect the UI to update whilst it's playing. But for anything longer than half a second, you need to play the sound from a background thread. Which is where theBackgroundWorker
comes in. SprinklingApplication.DoEvents()
calls throughout your code is a hack, and a sign of code which needs to be changed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Ok, if not using PlaySync instead use mciSendString with status command, will this command play sequentially ?
-
Ok, if not using PlaySync instead use mciSendString with status command, will this command play sequentially ?
As far as I can see,
mciSendString
doesn't wait for the sound to finish playing, so you'd be back to square one. And if it did wait, you'd still have to call it on a background thread to avoid freezing your UI. Just use aBackgroundWorker
and theSoundPlayer
class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As far as I can see,
mciSendString
doesn't wait for the sound to finish playing, so you'd be back to square one. And if it did wait, you'd still have to call it on a background thread to avoid freezing your UI. Just use aBackgroundWorker
and theSoundPlayer
class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thank you for answering my questions.