0

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

我想要实现的是在我单击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
4

2 回答 2

1

首先,您应该通过指定以下内容开始异步语音:

SpeechVoiceSpeakFlags.SVSFlagsAsync

然后你可以使用Skip停止演讲:

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

希望这可以帮助。

于 2013-06-20T11:40:49.670 回答
-1

您可以通过调用中止线程

t1.Abort()
于 2013-06-20T11:03:42.190 回答