Click here to Skip to main content
15,911,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem , I want to open main window and then i want the window to speak "welcome to email system " . I dont know what is wrong , it speaks this first and then the window appears , i want the window to appear and then say the message

my code
public partial class MainWindow : MetroWindow
{


private SpeechRecognitionEngine sre;
private SpeechSynthesizer synth;
public MainWindow()
{
InitializeComponent();


synth = new SpeechSynthesizer();
sre = new SpeechRecognitionEngine();
speak();
}


public void speak()
{

synth.SetOutputToDefaultAudioDevice();
synth.Speak("Welcome to email System");

What I have tried:

made object in the method of the same class and called the main window, didnt work
Posted
Updated 27-Sep-16 4:16am
Comments
[no name] 27-Sep-16 9:59am    
"it speaks this first and then the window appears", it does that because that is *exactly* what you told it to do.
"window to appear and then say the message", then use something other then the window constructor.
Isma Tipu 27-Sep-16 10:08am    
i dont know what is wrong , that is why i posted the question. something other wont help. how do i do it ? mainwindow() comes with the xaml. it loads when you create the class.Initializecomponent() should make the window appear first but it doesnt
[no name] 27-Sep-16 10:21am    
Ah so you don't know anything about event driven programming? InitializeComponent does NOT make the window appear. Get yourself a book or find some tutorials on basic event driven programming and work through them.
Philippe Mori 27-Sep-16 12:30pm    
InitializeComponent only create the object is memory. The UI is shown when Show function is called. And obviously, that function cannot be called before the constructor has executed since it is called on the returned object. It does not matters if you explicitly show the windows or the framework does it because it was specified to be used as the main window.
Karthik_Mahalingam 27-Sep-16 10:07am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

Speak is a blocking operation - it doesn't return until the method completes: SpeechSynthesizer.Speak Method (String) (System.Speech.Synthesis)[^]
You could replace it with the asynchronous method: SpeechSynthesizer.SpeakAsync Method (String) (System.Speech.Synthesis)[^] which may help, but I'd start by moving the code to the ContentRendered event rather than putting it in the constructor and then make it asnyc.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900