如何停止多线程讲话?

时间:2013-06-20 10:05:49

标签: vb.net multithreading

我有一个显示消息框的表单,同时启动了一个说同一消息的新线程。

我想要实现的是在我单击消息框上的OK按钮后立即停止说话消息的线程。

我一旦点击消息框上的OK,我就尝试使用abort命令终止该线程,但即使消息框消失,SPEAK过程仍然会保持整个消息的说话。

我假设新线程被abort命令杀死但是说话过程可能正在使用某个玩家说出该消息并且该玩家没有被停止。任何帮助将不胜感激。

以下是代码:

Imports SpeechLib

Public Class Form1


    Public voice As SpVoice = New SpVoice()
    Public speak As Boolean = True

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        Dim t1 As New Threading.Thread(AddressOf THREAD)
        t1.IsBackground = True
        t1.Start()
        MessageBox.Show("Data has not been saved yet. Are you sure you want to quit with out saving the chandes?")
        t1.Abort()

    End Sub

    Public Sub THREAD()

        If (speak = True) Then

            voice.Speak("Data has not been saved yet. Are you sure you want to quit with out saving the chandes?")

        End If

    End Sub

End Class

2 个答案:

答案 0 :(得分:1)

首先,您应该通过指定:

来启动异步语音
SpeechVoiceSpeakFlags.SVSFlagsAsync

然后你可以使用跳过

停止讲话
voice.Skip("Sentence", Int32.MaxValue)

这给出了:

Imports SpeechLib

Public Class Form1


    Public voice As SpVoice = New SpVoice()
    Public speak As Boolean = True

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        Dim t1 As New Threading.Thread(AddressOf THREAD)
        t1.IsBackground = True
        t1.Start()
        MessageBox.Show("Data has not been saved yet. Are you sure you want to quit with out saving the chandes?")
                    voice.Skip("Sentence", Int32.MaxValue)

    End Sub

    Public Sub THREAD()

        If (speak = True) Then

            voice.Speak("Data has not been saved yet. Are you sure you want to quit with out saving the chandes?", SpeechVoiceSpeakFlags.SVSFlagsAsync)

        End If

    End Sub

End Class

希望这有帮助。

答案 1 :(得分:-1)

您可以通过调用

中止线程
t1.Abort()