对于ListBox中的每个元素,启动任务并等待它完成后再继续

时间:2016-05-07 19:57:32

标签: .net vb.net

我有一个 ListBox ,其中包含大量元素。所以基本上我想要启动我的辅助类的实例,这是对主要工作的假设,当它完成时,转到ListBox中的下一个元素并重复该过程直到结束,同时显然保持GUI免费。我将发布我的中学班级的伪代码,让大家了解它的工作情况。

Imports System.Threading

Public Class Demo

    Private Ip As String
    Private Port As Integer
    Private SW As SomeWork  
    Private mre As New ManualResetEvent(False)

    Public Event OnPacket(ByVal Data As SomeMessage)

    Sub New(ByVal Ip As String, ByVal Port As Integer)
        'Self Explantory
    End Sub

    Sub Connect()
        SW = New SomeWork(IP, Port)
        SW.ProcessForMounting(AddressOf InitiateCallback, mre)
        Validate(mre)
    End Sub

    Private Sub Validate(ByVal mre As ManualResetEvent)
        'Some Work here
    End Sub

    Private Sub InitiateCallback(ByVal client As Example)
        client.BufferCallbacks(AddressOf SomeCallBack, mre)                 
    End Sub

    Private Sub SomeCallBack(ByVal Data As SomeMessage)
        'either this or raising the event on main gui
        Dim type As String = Data.Type.ToString()

        Select Case type
            Case is = "Done"
                ' Finished work
                ' Writes something to the server and calls Done() method
                ' On the calling class, move to the next element  
        End Select 
        'Or RaiseEvent Packet(Data)
    End Sub

    Public Sub Done()
        SW.Dispose()
        SW = Nothing
    End Sub

End Class

现在,问题是如果我在一个线程中放置 Connect()方法,它将在方法的最后一行之后退出而不等待 SomeCallBack() 接收完成

再一次,它只是一个伪代码

1 个答案:

答案 0 :(得分:0)

我认为最简单的方法是,应该逐个处理,以引入一个Boolean字段,指示最后一个回调是否已完成。可以在调用者端使用Connect()调用Await方法,以便UI不会冻结。

Imports System.Threading

Public Class Demo

    Private Ip As String
    Private Port As Integer
    Private SW As SomeWork  
    Private mre As New ManualResetEvent(False)

    ' Introduce a flag to indicate if the work is done
    Private processingFinished As Boolean = False

    Public Event OnPacket(ByVal Data As SomeMessage)

    Sub New(ByVal Ip As String, ByVal Port As Integer)
        'Self Explantory
    End Sub

    ' Instead of Sub Connect() we use the Async/Await feature
    ' so that this method result can be awaited.
    ' On the caller side use: Await Connect()
    Async Function Connect() As Task
        processingFinished = False
        SW = New SomeWork(IP, Port)
        SW.ProcessForMounting(AddressOf InitiateCallback, mre)
        Validate(mre)

        ' Waiting until work is done.
        While Not processingFinished
            Await Task.Delay(1000);
        End While
    End Sub

    Private Sub Validate(ByVal mre As ManualResetEvent)
        'Some Work here
    End Sub

    Private Sub InitiateCallback(ByVal client As Example)
        client.BufferCallbacks(AddressOf SomeCallBack, mre)                 
    End Sub

    Private Sub SomeCallBack(ByVal Data As SomeMessage)
        'either this or raising the event on main gui
        Dim type As String = Data.Type.ToString()

        Select Case type
            Case is = "Done"
                ' Finished work
                ' Writes something to the server and calls Done() method
                ' On the calling class, move to the next element  
        End Select 
        'Or RaiseEvent Packet(Data)
    End Sub

    Public Sub Done()
        SW.Dispose()
        SW = Nothing

        ' Here we indicate that we have finished processing the current item.
        processingFinished = True
    End Sub

End Class