Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I'm needing some help with the Speech Recognition thing. I basically put it into an application, that when you press a button, it starts listening to you.
The thing is that I can't NEVER abort the BackgroundWorker. It just never ends, and never stops, even thought it can only detect once and detects no more without restarting the program.
VB
Public Class AbortableBackgroundWorker
    Inherits BackgroundWorker

    Private _workerThread As Thread

    Protected Overrides Sub OnDoWork(e As DoWorkEventArgs)
        _workerThread = Thread.CurrentThread
        Try
            Using recognizer As New SpeechRecognitionEngine()
                Dim gb As New GrammarBuilder()
                gb.Culture = New System.Globalization.CultureInfo("en-US")
                gb.AppendDictation()
                Dim g As New Grammar(gb)
                g.Name = ("English Grammar")
                recognizer.LoadGrammarAsync(g)
                AddHandler recognizer.LoadGrammarCompleted, AddressOf recognizer_LoadGrammarCompleted
                AddHandler recognizer.SpeechDetected, AddressOf recognizer_SpeechDetected
                AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized
                AddHandler recognizer.RecognizeCompleted, AddressOf recognizer_SpeechFinished
                recognizer.SetInputToDefaultAudioDevice()
                recognizer.RecognizeAsync()
                ' Must use Console.ReadLine() if I want it to work properly!
                Console.ReadLine()
            End Using
        Catch generatedExceptionName As ThreadAbortException
            e.Cancel = True
            Thread.ResetAbort()
        End Try
    End Sub

    Public Sub Abort()
        If _workerThread IsNot Nothing Then
            _workerThread.Abort()
            _workerThread = Nothing
        End If
    End Sub
    Private sr As New Speech.Recognition.SpeechRecognitionEngine()
    Dim completed As Boolean
    Public Sub SetInputToDefaultAudioDevice()
    End Sub

    Public Sub recognizer_SpeechDetected(sender As Object, e As SpeechDetectedEventArgs)
        _Console.Register("Voice detected!")
    End Sub

    ' Handle the LoadGrammarCompleted event.
    Public Sub recognizer_LoadGrammarCompleted(sender As Object, e As LoadGrammarCompletedEventArgs)
        _Console.Register("Grammar loaded " & Convert.ToString(e.Grammar.Name))
    End Sub

    ' Handle the SpeechRecognized event.
    Public Sub recognizer_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
        _Console.Register("Voice! " & Convert.ToString(e.Result.Text))

    End Sub

    Private Sub recognizer_SpeechFinished(sender As Object, e As RecognizeCompletedEventArgs)
        _Console.Register("No more voice!")
        Me.Abort()
        If (Me.IsBusy = False) Then
            _Console.Register("False!")
        End If
    End Sub

End Class


Is there a way that I can restart the backgroundworker?
Thanks!
Posted
Updated 3-Nov-14 0:40am
v3
Comments
Sergey Alexandrovich Kryukov 2-Nov-14 20:33pm    
You never showed how did you create the thread (background worker is one one of the ways to create an extra threads and work with it), never showed how did you try to abort it. I have never had problems with that. However, you need to use appropriate apartment state for it (one recognize class can only work with STA, another one only with MTA). So, does the recognition work at all?
Besides, I advise you to use one permanent thread for recognition and use it during the lifetime of your process.
—SA
[no name] 3-Nov-14 6:39am    
Yep, corrected... sorry! And yes, it works, but like, only once. If I click the button for the first time, it works. I talk, it detects. After, if I try to click again in the button, an exception is thrown because the BackgroundWorker is already running.
Sergey Alexandrovich Kryukov 3-Nov-14 13:44pm    
You need to explain all the detail. Full exception information, especially the stack (in what like thrown, and so on...).
—SA
[no name] 3-Nov-14 15:43pm    
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: This BackgroundWorker is currently busy and can not run multiple tasks concurrently."

I had to translate from the portuguese to english

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