none
[ vb2005 ] 多執行緒停止 RRS feed

  • 問題

  • Dim ListenerThread As New Thread(AddressOf MainProcess)
        Sub MainProcess()
            Dim Ports() As String = {"http://*:8088/"}
            ListenerProcess(Ports)
        End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            TmrBroadCast.Enabled = Not TmrBroadCast.Enabled
            If TmrBroadCast.Enabled = True Then
                If ListenerThread Is Nothing Then ListenerThread = New Thread(AddressOf MainProcess)
                ListenerThread.Start()
                Button5.Text = "停止"
            Else
                Button5.Text = "廣播"
                ListenerThread = Nothing
            End If
        End Sub

    Public Sub ListenerProcess(ByVal prefixes As String())
            Static flag As Boolean = False
            If flag = True Then Exit Sub

            flag = True

            If Not HttpListener.IsSupported Then
                MsgBox("此系統需要 Windows XP 且安裝 SP2 以上版本 或  Server 2003 才可以使用.", MsgBoxStyle.Critical)
                End
            End If
            Dim listener As HttpListener = New HttpListener
            For Each s As String In prefixes
                listener.Prefixes.Add(s)
            Next
            listener.Start()
            Do While True
                Dim buffer() As Byte = Nothing
                Try
                    Application.DoEvents()
                    Dim context As HttpListenerContext = listener.GetContext
                    Dim request As HttpListenerRequest = context.Request
                    Dim response As HttpListenerResponse = context.Response
                    Do While buffer Is Nothing
                        Try
                            buffer = My.Computer.FileSystem.ReadAllBytes(Application.StartupPath & "\buffer.bmp")
                            Application.DoEvents()
                        Catch ex As Exception
                        End Try
                    Loop
                    response.ContentLength64 = buffer.Length
                    Dim output As Stream = response.OutputStream
                    output.Write(buffer, 0, buffer.Length)
                    output.Close()
                    buffer = Nothing
                Catch ex As Exception
                    Application.DoEvents()
                    buffer = Nothing
                End Try
                Application.DoEvents()
            Loop


        End Sub

    以上是我用來對外廣播(傳影像)的部分程式

    我原本沒有加
    Static flag As Boolean = False
    If flag = True Then Exit Sub
    flag = True

    結果只要我關掉廣播後再開啟就會跑出

    無法接聽前置詞 'http://*:8088/',因為它與電腦上的現有登錄衝突。

    請問一下那三行的用途是???

    2009年9月29日 下午 06:44

解答

所有回覆