Share via

Speech-to-text continuous recognition sample program sometimes stops working in the middle of WPF in non-vs debug mode

CodingNinja 96 Reputation points
2022-05-04T13:01:34.923+00:00

I try to use speech to text (continuous recognition method)use-continuous-recognition

I found that if the program is WPF and I run the exe program directly, there is a chance that the continuous recognition will stop working in the middle of the process, but if I use vs to debug the program, it always works.

I have done many tests on:
Console + .NET6 or .Net Framework4.8 => debug mode and run mode all work fine.
I found that only WPF programs have this problem.

public class Test  
{  
    private readonly TaskCompletionSource<bool> stopRecognition = new TaskCompletionSource<bool>();  
  
    public async Task TestAsync()  
        {  
            using (var audioConfig = AudioConfig.FromWavFileInput("test.wav"))  
            {  
                var speechConfig = SpeechConfig.FromSubscription("MY KEY", "eastus");  
                using (var recognizer = new SpeechRecognizer(speechConfig, audioConfig))  
                {  
                    recognizer.SessionStarted += (sender, e) => {  
                        Debug.WriteLine($"SessionStarted: {e.SessionId}");  
                    };  
  
                    recognizer.SpeechStartDetected += (sender, e) => {  
                        Debug.WriteLine($"SpeechStartDetected: {e.SessionId}");  
                    };  
  
                    recognizer.Recognizing += (s, e) => {  
                        Debug.WriteLine($"RECOGNIZING: Text={e.Result.Text}");  
                    };  
  
                    recognizer.Recognized += (s, e) => {  
                        if (e.Result.Reason == ResultReason.RecognizedSpeech)  
                        {  
                            Debug.WriteLine($"RECOGNIZED: Text={e.Result.Text}");  
                        }  
                        else if (e.Result.Reason == ResultReason.NoMatch)  
                        {  
                            Debug.WriteLine($"NOMATCH: Speech could not be recognized.");  
                        }  
                    };  
  
                    recognizer.Canceled += (s, e) => {  
                        Debug.WriteLine($"CANCELED: Reason={e.Reason}");  
  
                        if (e.Reason == CancellationReason.Error)  
                        {  
                            Debug.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");  
                            Debug.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");  
                            Debug.WriteLine($"CANCELED: Did you set the speech resource key and region values?");  
                        }  
  
                        stopRecognition.TrySetResult(true);  
                    };  
  
                    recognizer.SessionStopped += (s, e) => {  
                        Debug.WriteLine("\n    Session stopped event.");  
                        stopRecognition.TrySetResult(true);  
                    };  
  
                    await recognizer.StartContinuousRecognitionAsync();  
  
                    await stopRecognition.Task;  
                }  
            }  
        }  
}  


<Grid>  
     <TextBox x:Name="output" />  
</Grid>  

private async void Window_Loaded(object sender, RoutedEventArgs e)  
{  
    output.Text = "start";  
  
    var test = new Test();  
    await test.TestAsync();  
  
    output.Text += "\ndone.";  // if in vs-debug mode, it will print "done.", but if run exe directly, sometimes it will not.  
}  

UPDATE:
I tried adding .ConfigureAwait(false) for all asynchronous operations and the problem remained, but if I also moved the TestAsync method to code-behind to use it, the problem was solved!

Why on earth would this be so? I was troubled.

1482

Community Center | Not monitored

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.