Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my MainWindow.xaml.cs:
C#
using statements...
....;
namespace xyz
{
 class abc :Window
 {
  public SpeechSynthesizer qwe;
  public abc()
  {
   InitializeComponent();
   qwe = new SpeechSynthesizer();
   qwe.Speak("Hello");
  }
 }
}

The problem is that speech is synthesized before the window is diaplayed.
i want the window to load all its contents and then speak the text.
thanks for your help
regards
Ratul :-)

What I have tried:

I wasent able to get help from google ... and so now im here.
Posted
Updated 2-Jun-16 5:05am

1 solution

Don't call the Speak method until the window's Loaded event fires:
C#
public abc()
{
    InitializeComponent();
    qwe = new SpeechSynthesizer();
    Loaded += Window_Loaded;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    qwe.Speak("Hello");
}

If that's still too soon, use the Dispatcher:
C#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Dispatcher.BeginInvoke((Action)() => qwe.Speak("Hello")), DispatcherPriority.ContextIdle, null);
}

Alternatively, use the SpeechSynthesizer.SpeakAsync method[^]:
C#
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    qwe.SpeekAsync("Hello");
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jun-16 11:39am    
5ed.
Maybe asynchronous synthesis is not the alternative but the addition to handling the event.
—SA
Ratul Thakur 2-Jun-16 12:19pm    
Solved it .. thanks :-)

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