Microsoft SpeechSynthesizer - How do I pass parameters to SpeakCompleted?

23 Views Asked by At

I'm looking to pass additional parameters such as a string and bool to the SpeakCompleted event handler. Can you please help me know the proper way?

SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);

static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
   // Looking to get a string & bool here.
   Console.WriteLine("\nThe SpeakAsync operation was cancelled!!");
}

Thank you!

1

There are 1 best solutions below

0
Paul-Jan On

As noted in the comments to your question, you cannot directly add information to an event defined by 3rd party code... so you'll need other ways to get the information to your event handler.

The usual way to do this is using class fields, i.e. define

private bool _myBool;
private string _myString;

at the top of your class, set them when the information become available, and then in the event

static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
  // You can now access _myBool and _myString here
}

Once you do this it's often nicer to split the synthesizer handling off into a separate class, so these fields don't pollute the rest of your code.