线程的状态

时间:2013-07-29 15:30:59

标签: vb.net multithreading

我在我的应用程序中使用多线程概念。我正在使用下面的代码

 Dim threadHistory As Thread = Nothing
                For Each dRow As DataRow In sqlDS.Tables(0).Rows
                    GetPropertyBidHistory(dRow("ID"))
                    threadHistory = New Threading.Thread(AddressOf GetRowHistory)
                    threadHistory.Name = "Row" + dRow("ID")    
                    threadHistory.Start(dRow("ID"))                       
                    threadHistory.Join()
                Next

    Public Sub GetRowHistory(ByVal ID As String)
            '1 min code from web srvice
    End Sub

如果我有10个Id,我怎么知道是否所有10个线程都已完成。

1 个答案:

答案 0 :(得分:0)

你正在开始并一个接一个地加入这个主题。那也许不是你的意图。如果你保持这种方式,你创建一个单独的线程,等待它完成,然后才进入下一个元素。

我会尝试以下操作:对于每个线程,将其添加到列表或数组中,然后在For Each / Next语句之后,您可以使用该属性将它们全部加入,我猜,JoinAll()

Dim List( Of Thread) allThreads  = new List
Dim threadHistory As Thread = Nothing

                For Each dRow As DataRow In sqlDS.Tables(0).Rows
                    GetPropertyBidHistory(dRow("ID"))
                    threadHistory = New Threading.Thread(AddressOf GetRowHistory)
                    threadHistory.Name = "Row" + dRow("ID")    
                    allThreads.Add(threadHistory)
                    threadHistory.Start(dRow("ID"))                       

                Next
Thread.JoinAll(allThreads) 'Blocks until all the threads finish
相关问题