加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB.Net程序设计:用队列控制多线程。

发布时间:2020-12-17 08:20:24 所属栏目:百科 来源:网络整理
导读:Queuing Threads from the Generic Queue Class [VB.NET/C# Examples by AceInfinity] From: http://www.sysnative.com/forums/programming/1117-queuing-threads-generic-queue-class-%5Bvbulletin-net-c-examples-aceinfinity%5D.html Here's an example

Queuing Threads from the Generic Queue Class [VB.NET/C# Examples by AceInfinity]


From: http://www.sysnative.com/forums/programming/1117-queuing-threads-generic-queue-class-%5Bvbulletin-net-c-examples-aceinfinity%5D.html

Here's an example I put together for someone on MSDN who wanted to be able to easily create a queue of methods to start new threads,and be able to start them one after another in order. Thought i'd share it here as well,as this can be very useful for managing a Thread Queue. Thread syncrhonization in an application is valuable,and can get somewhat complex as well...
用队列控制多线程。也有一定的用途。

Public Class Form1

    Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load
        
    End Sub

    Private qt As Queue(Of Thread)
    Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click
        qt = New Queue(Of Thread)
        qt.Enqueue(New Thread(AddressOf method1))
        qt.Enqueue(New Thread(AddressOf method2))
        qt.Enqueue(New Thread(AddressOf method3))
        qt(0).Start()
    End Sub

    Private Sub UpdateQueue()
        qt.Dequeue()
        Sync.Variable = qt.Count
    End Sub

    Private Sub method1()
        Console.WriteLine("method1")
        Thread.Sleep(3000)
        UpdateQueue()
    End Sub

    Private Sub method2()
        Console.WriteLine("method2")
        Thread.Sleep(3000)
        UpdateQueue()
    End Sub

    Private Sub method3()
        Console.WriteLine("method3")
        Thread.Sleep(3000)
        UpdateQueue()
    End Sub

    Private WithEvents Sync As New SyncQueueCount
    Private Sub VariableChanged(ByVal QCount As Integer) Handles Sync.VariableChanged
        If QCount > 0 Then
            qt(0).Start()
        Else
            MessageBox.Show("Queue Finished")
        End If
    End Sub

End Class

Public Class SyncQueueCount
    Private S_Int As Integer
    Public Event VariableChanged(ByVal value As Integer)

    Public Property Variable() As Integer
        Get
            Variable = S_Int
        End Get
        Set(ByVal value As Integer)
            S_Int = value
            RaiseEvent VariableChanged(S_Int)
        End Set
    End Property
End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读