Hello I have developed a program to speak the contents of a web page.
Here is the code i do this with:
public void SpeakSynthText(String TextToSpeak, bool SpeechRecognitionEnable)
{
#region SpeechSynth
if (SpeechRecognitionEnable == true)
{
this.Start();
}
SpeechSynthesizer synth = new SpeechSynthesizer();
if (Boolean.Parse(SpeechSaveEnabled) == true)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "wave files (*.wav)|*.wav|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
TB.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
WaveFileLoc = saveFileDialog1.FileName;
synth.SetOutputToWaveFile(WaveFileLoc);
synth.Rate = -1;
}
}
synth.SelectVoiceByHints(VoiceGender.Female);
synth.Speak(TextToSpeak);
synth.Dispose();
verbalcommandmode = "true";
if (Boolean.Parse(SpeechSaveEnabled) == true)
{
System.Media.SoundPlayer m_SoundPlayer =
new System.Media.SoundPlayer(@WaveFileLoc);
m_SoundPlayer.Play();
m_SoundPlayer.Dispose();
}
#endregion
}
when i want to speak the contents of a web page i pass the arguments of:
this.SpeakSynthText(WIKI.Document.Body.InnerText, false);
now when i run this the UI freezes up until its done speaking, so i was told that i would have to run the function on a thread separate of the UI thread.
So i read all the articles i could find on codeproject and this is what i got:
private void SpeakPageText_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(RunInThread));
t.Start();
}
this function(sorry i keep calling them functions i am used to coding in python) is called when i click the button associated with this method
when i run this method it is SUPPOSED to run the RunInThread method in a new thread. The RunInThread method goes like this:
public void RunInThread()
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(delegate { this.SpeakSynthText(WIKI.Document.Body.InnerText, false); }));
}
}
catch (InvalidOperationException oex)
{
MessageBox.Show(oex.Message);
}
}
i have a feeling that it isn't running RunInThread on a separate thread but instead on the main thread, so how do run this method on a new thread?
or if that won't work how can i speak text with the speech synthesizer but not have the UI freeze up?
All of your help is very much appreciated.