|
I have the following C # statement:
...
using (SoundPlayer player = new SoundPlayer(Properties.Resources.ring))
{
player.Play();
while (If the bell is running, wait here)
{
if (player.Stop()==true)
{
this.progressBarControl1.Visible = false;
}
}
}
...
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 a BackgroundWorker instance[^] - put the SoundPlayer code in the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted 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
|
|
|
|
|
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)
{
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 the DoWork event handler, and the code to hide the progress bar in the RunWorkerCompleted event handler.
You've put all of the code in the button's Click event handler instead.
And as I said, you'll need to use the PlaySync 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");
}
|
|
|
|
|
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 the BackgroundWorker comes in.
Sprinkling Application.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 ?
|
|
|
|
|
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 a BackgroundWorker and the SoundPlayer 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.
|
|
|
|
|
In Visual Studio C#. I cannot find a Text To Speech library in languages other than English, does Visual Studio C# support the Text To Speech library for Vietnamese ? or 3rd party supported libraries for C# language.
|
|
|
|
|
|
The links you sent to me do not support Vietnamese language.
|
|
|
|
|
The following languages are now available for text to speech, with new languages in bold:
Arabic Croatian French Indonesian Polish Spanish
Bulgarian Czech German Italian Portuguese Swedish
Cantonese Danish Greek Japanese Romanian Tamil
Catalan Dutch Hebrew Korean Russian Thai
Chinese (Simplified) English Hindi Malay Slovak Turkish
Chinese (Traditional) Finnish Hungarian Norwegian Slovenian Vietnamese
|
|
|
|
|
I never tried out this software, and time doesn't allow me to learn the API to write an application using it. Is this functionality integrated into some freely available application?
For a couple of years, a hobby of mine has been collecting Norwegian homographs, ie. words with identical spelling but different meaning and pronounciation. So I have written a text competing with "English is tough stuff": To entertain a party, ask one of those present to read the text out loud (with no preparations). I find it difficult myself to get everything right... I'd like to try out that text on some speech generator, to see how well it succeeds in determining the right pronounciation from context. (In a couple of places, two different pronounciations are valid, even in the same context, but the meaning is different. Most places, selecting the wrong pronounciation results in a meaningless statement.) Note that this is a Norwegian text; translation of such stuff is usually meaningless.
(My early interest in homographs were triggered in my teenage days, when I didn't know English that well, especially rock music terminology. So when I saw this LP cover where one guy was listed as playing lead guitar, I made a laugh, believing that it was a joking term for a heavy bass guitar, heavy as lead! I pointed it out to my buddies, and they thought I was making a really great joke - they never realized that I had truly misunderstood and misread the term!)
|
|
|
|
|
Member 7989122 wrote: Is this functionality integrated into some freely available application? Sorry, no idea.
|
|
|
|
|
Hi Richard MacCutchan!
I am mistaken, in the list of Vietnamese language support, Apparently only support Vietnamese online ?
While I want to find the *.dll library that can be added to the program that runs offline, did you find the library to use offline for the C# programming language ?
|
|
|
|
|
Sorry, but you will have to follow the links in that blog for the information, this is not something I have used.
|
|
|
|
|
I am writing a C# application that can control, monitor and upgrade firmware on 3 different hardware products. These products are accessed through registers and every once in a while registers are made obsolete or added (at the end). In my C# application I have a table of the latest versions this version of the C# application supports, e.g.:
PRODUCT_0 3.0.2.8
PRODUCT_1 2.0.2.1
PRODUCT_2 4.4.2.0
If I try to communicate with a PRODUCT_1 of version 2.0.2.2 in the example above, my C# application would display an error message asking the user to obtain a newer version of the C# application.
I feel it would be nice to also give the actual C# application some kind of version or unique identifier. Of course, in the example above I could call it "version" 3.0.2.8 2.0.2.1 4.4.2.0, but as I add more products this "version" would become very long. I could use the same versioning format as the products (W.X.Y.Z), but is becomes difficult to manage when several persons work on separate branches on the different products in Git. I'm thinking that calculating a 4-byte CRC/hash on the supported versions would be a better option, but then I lose chronological information (it becomes hard to know which version is the latest version). So that would need to supplemented with a timestamp of some sort. Please note that I want the version/hash/time stamp to be compiled into the C# so it can be shown in a little "About"-box. Does anybody have any suggestions?
Another thing I was thinking of is to include the entire Visual Studio source code of my C# application project as a zip-file that can be downloaded from within my C# application by pressing a button "Get This Application's Source Code", that way I could always get the source code of an .exe file and then I could iterate through Git to find the most appropriate commit. Does anybody have experience with this and can share experiences?
|
|
|
|
|
I use a "minimum supported version" for new functionality. That version refers to the executable, which has it's build-number (part of the version) updated autmatically. When checking if something is available for the "current" version, I just check the first three and ignore the build altogether.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
arnold_w wrote: Please note that I want the version/hash/time stamp to be compiled into the C# so it can be shown in a little "About"-box.
You could use attributes for that matter. It would allow you to insert metadata in the executable about which version is supported for which product.
Attributes (C#)[^]
Then, you can either access this metadata at runtime using reflection (and format proper error messages, for example), or inspect the assembly with ildasm.exe and get these informations.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
This may help with some of it: Timestamping assemblies with Build date and time.[^] - the use of pre- and post- build events would also allow you to embed the zipped source code of your app in your app as a resource in much the same way.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I like the T4 Template solution proposed by Peter Taylor on SO:
c# - Displaying the build date - Stack Overflow[^]
There are also several other interesting approaches in that thread.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Team,
I am working to fix the issue of Cross Side Scripting-XSS in asp.net with C#. team is using the burp interceptor to inject the script like this %uff1cscript%uff1ealert%uff08123456%uff09%uff1c/script%uff1e but I am tried several ways to encode this text to find out the html but I am not able to do it tried antixss and html encode but its not working.
please let us know how to handle these codes to fix the issue
|
|
|
|