I am adding speech synthesis to my application. This works, but the problem is that I canβt cancel the speech ... For example, when I go to another page, the speech continues ... So, I call the CancelAll () method to cancel the current speech, but an exception occurs and I donβt know why. Do you know what the problem is?
An exception
A first chance exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary The program '[2576] TaskHost.exe' has exited with code -1 (0xffffffff).
My code is:
private SpeechSynthesizer synth = new SpeechSynthesizer(); protected override void OnBackKeyPress(CancelEventArgs e) { //I tried to cancel also here but it the same exception... } //method called when I press a button Cancel private void ButtonCancelSpeech(object sender, EventArgs eventArgs) { try { synth.CancelAll(); } catch (TaskCanceledException) { //I arrive in this exception } } private async void BtnSpeech_Click(object sender, EventArgs e) { IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All where voice.Language.Substring(0, 2).Equals(LanguageApp.GetLangage2Characters()) select voice; if (voices.ElementAt(0) != null) { // Set the voice as identified by the query. synth.SetVoice(voices.ElementAt(0)); await synth.SpeakTextAsync(_place.Description); } }
thanks
source share