回调任务

时间:2013-03-27 07:06:48

标签: vb.net visual-studio

我使用以下代码

创建了一个任务
For (//pls ignore the variables)
            Dim task = (New System.Threading.Tasks.TaskFactory()).StartNew(
            Sub()
                downloadFiles()
            End Sub
            )

Next

任务创建了很多次,并且它可以异步工作。现在我不知道如何为此添加回调方法。谁能告诉我如何为此添加回调?

1 个答案:

答案 0 :(得分:2)

回调的使用应如下所示: 我遗漏了......每个人。

 Private Sub StartTask_Click(sender As Object, e As RoutedEventArgs) Handles StartTask.Click
    Dim LongTask As Task = (New System.Threading.Tasks.TaskFactory).StartNew(AddressOf DoWorkAsync)
    LongTask.ContinueWith(AddressOf WorkCompleted)
    Debug.Print("")
  End Sub

  ''' <summary>
  ''' Called after DoWorkAsync Task is complete
  ''' </summary>
  ''' <remarks></remarks>
  Private Sub WorkCompleted()
    Debug.Print("completed")
  End Sub

  ''' <summary>
  ''' Called by Task
  ''' </summary>
  ''' <remarks></remarks>
  Public Sub DoWorkAsync()
    For i = 0 To 2000
      Debug.Print("test")
    Next
  End Sub