Click here to Skip to main content
14,541,223 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
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)
        //starts the speech engine

    // Initialize a new instance of the SpeechSynthesizer.
    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.Rate = -1;


    // Select a voice that matches a specific gender.
    // Speak a text string synchronously and play back the output file.

    verbalcommandmode = "true";
    if (Boolean.Parse(SpeechSaveEnabled) == true)
        System.Media.SoundPlayer m_SoundPlayer =
        new System.Media.SoundPlayer(@WaveFileLoc);

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));



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()

    //Option 1

        if (this.InvokeRequired)
            this.Invoke(new EventHandler(delegate { this.SpeakSynthText(WIKI.Document.Body.InnerText, false); }));

    catch (InvalidOperationException oex)


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.
Updated 24-Nov-14 10:08am
Rate this:
Please Sign up or sign in to vote.

Solution 2


You don't need separate thread. All you need to call method:


Few links:[^][^][^]

And you have few events to subscribe:[^][^][^]

Two years ago I wrote post about MS Text-To-Speech. Article is in polish language but source code is in english. You can download source code here:[^]
Binary files here:[^]
Blog post is located here:[^]

Sample project allows you to play all or selected text from TextBox using default windows sound output. You can select voice from voices available on your system, get info about selected voice and control playback speed rate and volume. It can save output as WAV files as well. You can configure WAV file options such as bits per sample, channels and quality. Also it displays informations about playback progress, position etc.

I hope you find this useful :)
MasterCodeon 25-Nov-14 12:04pm
Could you send me the source for that text to speech article you did(i can't access the link)if you could that would be great because i need a way to speak the selected text not just all text.
if you can just send me a PM with it or something.
please and thank you.
MasterCodeon 25-Nov-14 12:04pm
Oh and by the way it worked like a charm thanks again!
Marcin Kozub 25-Nov-14 12:36pm
I'm glad that I helped you :)
Rate this:
Please Sign up or sign in to vote.

Solution 1

SP.Speak("Hello world!", SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
MasterCodeon 24-Nov-14 16:12pm
could you elaborate please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100