none
VB.NET中,如何中断或停止一个委托? RRS feed

  • 问题

  •  

    Module Module1
        Public BY_busy As Boolean
        Public GBDM As String
        Public Declare Function sndPlay Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal SoundName As    String,  ByVal Flags As Long) As Long
        Public Sub CBY_PLAY(ByVal BY As String)
            Dim path, GBxx() As String
            Dim intX As Integer
            path = Application.StartupPath & "\..\wav\"
            GBxx = Split(BY, ":")
            For intX = 0 To GBxx.Length - 1
                sndPlay(path & GBxx(intX) & ".wav", 0)
            Next
        End Sub
       
        Public Delegate Sub CBY_PLAYDelegate(ByVal Path As String)
        Public CBY_PLAYAsyncCallBack As AsyncCallback
        Public Sub CBY_PLAY_CallBack(ByVal b As IAsyncResult)
            If b.IsCompleted Then
                MsgBox("本信息已播放完毕!", 64, "广播完毕!")
                BY_busy = False
            End If
        End Sub
    End Module
      我的代码如上,主要是通过委托方式由后台播放声音。问题是当我认为播放错误时,我想立刻停止播放,应该如何写代码?
    2008年7月14日 8:30

答案

全部回复

  • CBY_PLAY_CallBack在哪用?

    2008年7月14日 9:07
    版主
  •  

    Module Module1
        Public BY_busy As Boolean
        Public GBDM As String
        Public Declare Function sndPlay Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal SoundName As String, ByVal Flags As Long) As Long
        Public Sub CBY_PLAY(ByVal BY As String)
            Dim path, GBxx() As String
            Dim intX As Integer
            path = Application.StartupPath & "\..\wav\"
            GBxx = Split(BY, ":")
            For intX = 0 To GBxx.Length - 1
                sndPlay(path & GBxx(intX) & ".wav", &H2)
            Next
        End Sub
        Public Sub STOP_PLAY(ByVal by As String)
            sndPlay(vbNullString, 0)
        End Sub
        Public Delegate Sub CBY_PLAYDelegate(ByVal Path As String)
        Public CBY_PLAYAsyncCallBack As AsyncCallback
        Public Delegate Sub STOP_PLAYDelegate(ByVal Path As String)
        Public STOP_PLAYAsyncCallBack As AsyncCallback
        Public Sub CBY_PLAY_CallBack(ByVal b As IAsyncResult)
            If b.IsCompleted Then
                MsgBox("本信息已播放完毕!", 64, "广播完毕!")
                BY_busy = False
            End If
        End Sub
    End Module

    完整的代码如上!

    2008年7月14日 9:39
  •  

    If iType = MsgBoxResult.Yes Then
                            If BY_busy = False Then
                                BY_busy = True
                                Grid1.Cell(e.Row, 11).Text = Grid1.Cell(e.Row, 11).Text + 1
                                Grid1.Cell(e.Row, 12).Text = FormatDateTime(Now, DateFormat.ShortTime)
                                CBY_PLAYAsyncCallBack = New AsyncCallback(AddressOf CBY_PLAY_CallBack)
                                CBY_PLAYThread.BeginInvoke(GBDM, CBY_PLAYAsyncCallBack, Nothing)
                            Else
                                MsgBox("系统正在广播,请稍后操作!", 64, "正在广播!")
                            End If
                        End If
                    End If

    这是调用的代码!

    2008年7月14日 9:43
  • 可以改用BackgroundWorker

    2008年7月15日 4:29
    版主