2023年11月20日 星期一

VB.NET Thread 多緒回傳

 ************Thread沒參數************


Public Class Class1

    Event EvnetRecall As EventHandler '用事件回傳結果

    Sub Add()

        Dim Show As Integer = 0

        Dim Add As Integer = 1

        For i = 0 To 10000 Step Add

            Show += i

        Next i

        Debug.WriteLine("Add執行緒:" & System.Threading.Thread.CurrentThread.ManagedThreadId)

        RaiseEvent EvnetRecall(Show, EventArgs.Empty)

    End Sub

End Class



Public Class Form1

    Dim A As New Class1


#Region "多緒"

    Public Sub TH_Add()

        Dim Thread1 As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf A.Add)) '設定Class為多緒

        Thread1.Name = "TH_Add"

        Thread1.Start()

    End Sub


    Public Delegate Sub TH_Add_Show(ByVal sander As Integer) '宣告委派TH_Add_Show


    Sub TH_AIGroupData_EventHandler(ByVal sender As Integer, ByVal e As System.EventArgs) '委派事件接收

        Me.BeginInvoke(New TH_Add_Show(AddressOf TH_Add_Show_Do), sender)

    End Sub

    Public Sub TH_Add_Show_Do(ByVal sender As Integer) '接收到事件後要執行的動作

        TextBox1.Text = sender

    End Sub

#End Region


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        AddHandler A.EvnetRecall, AddressOf TH_AIGroupData_EventHandler '共用事件

        TH_Add()

    End Sub

End Class









************Thread有參數************



Public Class Class1

    Event EvnetRecall As EventHandler '用事件回傳結果


    Public Structure StarAndStep

        Public Start As Integer

        Public StepInt As Integer

    End Structure


    Sub Add(ByVal Obj As StarAndStep)

        Dim Show As Integer = 0

        Dim Add As Integer = 1

        For i = 0 To 10000 Step Obj.StepInt

            Obj.Start += i

        Next i

        Debug.WriteLine("Add執行緒:" & System.Threading.Thread.CurrentThread.ManagedThreadId)

        RaiseEvent EvnetRecall(Obj.Start, EventArgs.Empty)

    End Sub

End Class



Public Class Form1


    Dim A As New Class1

#Region "多緒"

    Public Sub TH_Add(ByVal Obj As Object)

        Dim Thread1 As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf A.Add)) '設定副程式為多緒

        Thread1.Name = "TH_Add"

        Thread1.Start(Obj)

    End Sub


    Public Delegate Sub TH_Add_Show(ByVal sander As Object) '宣告委派TH_Add_Show


    Sub TH_AIGroupData_EventHandler(ByVal sender As Object, ByVal e As System.EventArgs) '委派事件接收

        Me.BeginInvoke(New TH_Add_Show(AddressOf TH_Add_Show_Do), sender)

    End Sub

    Public Sub TH_Add_Show_Do(ByVal sender As Object) '接收到事件後要執行的動作

        TextBox1.Text = sender

    End Sub

#End Region


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        AddHandler A.EvnetRecall, AddressOf TH_AIGroupData_EventHandler '共用事件

        Dim StarStep As New Class1.StarAndStep

        StarStep.Start = 0

        StarStep.StepInt = 2

        TH_Add(StarStep)

    End Sub

End Class


沒有留言:

張貼留言