VB.NET将集合中的每个项目与集合中的每个其他项目进行比较 - 线程

时间:2016-06-01 22:28:18

标签: vb.net multithreading

这是我的第一次发帖所以请接受我的道歉,如果我不这样做,请随时纠正我的任何格式或发布指南。我在VB.Net中使用.NET Framework 4.5.2进行此操作。

我在一个类中有一个名为gBoard的大型集合。

Private gBoard As Collection

它包含大约2000个类的实例。

我想要实现的是对于班级中的每个项目,我想查看班级中的每个其他项目,然后根据第二个项目中的变量更新第一个项目。

目前我有以下代码:

在主要课程中:

Private gBoard As New Collection ' This is populated elsewhere in the code 

Private Sub CheckSurroundings()
    For i As Integer = 1 To (xBoxes)
        For j As Integer = 1 To (yBoxes)
            For x = 1 As Integer To (xBoxes)
                For y = 1 As Integer To (yBoxes)
                    Tile(New Point(i, j)).CheckDistance(Tile(New Point(x, y)))
                Next y
            Next x
        Next j
    Next i
End Sub

Private Function Tile(ByVal aPoint As Point) As clsTile
    Return gBoard.Item("r" & aPoint.Y & "c" & aPoint.X)
End Function

在clsTile中我有以下(以及其他项目):

Private Function SurroundingTerrain(ByVal aTer As String) As clsTerrain
    Return mySurroundings.Item(aTer) ' a small collection (6 items of clsTerrain type) 
End Function

Public Sub CheckDistance(ByRef aTile As clsTile)
    SurroundingTerrain(aTile.Terrain).CheckDistance(CalcDistance(Location, aTile.Location))
End Sub

Private Function CalcDistance(ByVal myPoint As Point, ByVal aPoint As Point) As Double
    Dim myReturn As Double = 0
    Dim xDiff As Integer = 0
    Dim yDiff As Integer = 0
    Dim tDiff As Integer = 0
    xDiff = Math.Abs(myPoint.X - aPoint.X)
    yDiff = Math.Abs(myPoint.Y - aPoint.Y)
    tDiff = xDiff + yDiff
    myReturn = (MinInt(xDiff, yDiff) * 1.4) + (tDiff - MinInt(xDiff, yDiff))
    Return myReturn
End Function

Private Function MinInt(ByVal a As Integer, ByVal b As Integer) As Integer
    Dim myReturn As Integer = a
    If b < myReturn Then
        myReturn = b
    End If
    Return myReturn
End Function
clsTerrain中的

我有以下名为sub的子:

Public Sub CheckDistance(ByVal aDist As Double)
    If aDist < Distance Then
        Distance = aDist
    End If
End Sub

这运行和工作文件,但你可以猜测它运行得如此之慢......我一直在努力弄清楚如何使这个运行更快,我看着线程/任务但它似乎没有工作。没有错误,但对象似乎没有正确更新(或根本没有)。我试过的代码是:

在主要课程中:

Private Sub CheckSurroundings()
    Dim tasks As New List(Of Task)
    Dim pTile As clsTile
    For Each pTile In gBoard
        tasks.Add(Task.Run(Sub() TileToCheck(pTile)))
    Next
    Task.WaitAll(tasks.ToArray())
End Sub

Private Sub TileToCheck(ByRef aTile As clsTile)
    For x As Integer = 1 To (xBoxes)
        For y As Integer = 1 To (yBoxes)
            aTile.CheckDistance(Tile(New Point(x, y)))
        Next y
    Next x
End Sub

有没有人对如何使其发挥作用有任何建议或想法?

对于任何因头痛或面部疼痛而感到抱歉...

0 个答案:

没有答案