2016-05-07 8 views
0

ListBoxには多くの要素が設定されています。だから、基本的に私は、主な仕事と仮定して、それが完了したら、ListBoxの次の要素に移動し、明らかにGUIを解放しながら、最後までプロセスを繰り返すことを考えているインスタンスのインスタンスを開始したい。セカンダリクラスの擬似コードを投稿して、それが動作していることをすべて理解できるようにします。私は、Connect()のスレッドで方法を置けばListBox内の各要素に対して、タスクを開始し、移動する前にタスクが完了するまで待ちます。

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 

さて、問題は、が

を完了受信する SomeCallBack()を待たずにメソッドの最後の行の後に終了するだろう、です

はもう一度それだけの擬似コード

答えて

0

ある処理は、最後のコールバックが仕上げを持っているかどうかを示すBooleanフィールドを導入する一つ一つを行うべきである場合、私は最も簡単な方法を考えますed。 Connect()メソッドは、UIがフリーズしないように、呼び出し側でAwaitで呼び出すことができます。

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 
関連する問題