why this winform hangs and become unresponsive? RRS feed

  • Question

  • I have this visual basic winform which converts speech to text. This winform has 1 button and one textbox and uses azure Speech to text service. When I click button , It converts speech to text and shows in textbox but the problem is, the winform starts hanging and i cant do anything else and not even able close the winform. Anyone know what is going wrong? Thanks in advance.


    Imports Microsoft.CognitiveServices.Speech
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End Sub
        Public Async Function SpeechContinuousRecognitionAsync() As Task
            Dim config = SpeechConfig.FromSubscription("api key", "westus")
            Using recognizer = New SpeechRecognizer(config)
                AddHandler recognizer.SessionStarted, AddressOf recognizer_sessionstarted
                AddHandler recognizer.SessionStopped, AddressOf recognizer_sessionstopped
                AddHandler recognizer.Recognizing, AddressOf recognizer_Recognizing
                AddHandler recognizer.Recognized, AddressOf recognizer_Recognized
                AddHandler recognizer.Canceled, AddressOf recognizer_Canceled
                Await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(True)
            End Using
        End Function
        Private Sub recognizer_sessionstarted(ByVal sender As Object, ByVal e As SessionEventArgs)
            Console.WriteLine("\n    Session started event.")
        End Sub
        Private Sub recognizer_sessionstopped(ByVal sender As Object, ByVal e As SessionEventArgs)
            Console.WriteLine("\n    Session stopped event.")
        End Sub
        Private Sub recognizer_Recognizing(ByVal sender As Object, ByVal e As SpeechRecognitionEventArgs)
            Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}")
        End Sub
        Private Sub recognizer_Recognized(ByVal sender As Object, ByVal e As SpeechRecognitionEventArgs)
            TextBox1.Text = e.Result.Text
        End Sub
        Private Sub recognizer_Canceled(ByVal sender As Object, ByVal e As SpeechRecognitionCanceledEventArgs)
            Console.WriteLine($"\n    Recognition Canceled. Reason: {e.Reason.ToString()}, CanceledReason: {e.Reason}")
        End Sub
    End Class

    Sunday, October 20, 2019 12:02 PM

All replies

  • Hi

    I have just tried the following, and,apart from the accent errors, seems to,work OK. Not using the azure Speech to text service that you are using, but does the recognition and sets the text accordingly.

    Option Strict Off
    Option Explicit On
    Imports System.Speech.Recognition
    Public Class Form1
    	Private WithEvents listener As New SpeechRecognizer
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		listener.LoadGrammar(New DictationGrammar)
    	End Sub
    	Private Sub listener_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs) Handles listener.SpeechRecognized
    		TextBox1.Text = e.Result.Text
    	End Sub
    End Class

    Regards Les, Livingston, Scotland

    • Edited by leshay Sunday, October 20, 2019 3:05 PM
    • Proposed as answer by simonb549 Sunday, October 20, 2019 3:10 PM
    Sunday, October 20, 2019 1:14 PM
  • In general Wait does just that, wait until the task has completed.

    Example, GetServersAsync method uses ConfigureAwait(False) which keeps the app responsive.

    Dim servers = Await ops.GetServersAsync().ConfigureAwait(True)

    Don't use Wait!!!

    Try this

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Await SpeechContinuousRecognitionAsync().ConfigureAwait(True)
    End Sub

    Here is an example not using speech but is a full example:

    Imports Microsoft.SqlServer.Management.Smo
    Public Class DatabaseUtility
        Public Async Function GetServersAsync() As Task(Of List(Of String))
            Dim serverNames = New List(Of String)()
            Await Task.Run(
                    serverNames = SmoApplication.
                              AsEnumerable().Select(Function(row) row.Field(Of String)("Name")).
                End Sub).ConfigureAwait(True)
            Return serverNames
        End Function
    End Class


    Public Class Form1
        Private Async Sub GetServerNamesButton_Click(sender As Object, e As EventArgs) _
            Handles GetServerNamesButton.Click
            GetServerNamesButton.Enabled = False
            Dim ops As New DatabaseUtility
            Dim servers = Await ops.GetServersAsync().ConfigureAwait(False)
            If servers.Count > 0 Then
                ListBox1.Invoke(Sub() ListBox1.DataSource = servers)
                GetServerNamesButton.Invoke(Sub() GetServerNamesButton.Enabled = True)
            End If
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Sunday, October 20, 2019 2:48 PM
  • Hi,

    I think the above reply can provide you with a solution, have you tried it?

    Also, this document that performs recognition on the audio in a .wav file and writes the recognized text will be helpful too.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, October 21, 2019 6:15 AM