1

我使用以下代码创建了一个任务

For (//pls ignore the variables)
            Dim task = (New System.Threading.Tasks.TaskFactory()).StartNew(
            Sub()
                downloadFiles()
            End Sub
            )

Next

该任务被创建了 n 次,并且异步工作正常。现在我不知道如何为此添加回调方法。谁能告诉我如何为此添加回调?

4

1 回答 1

2

Callback 的使用应该如下所示:我省略了 for...each。

 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
于 2013-03-27T08:00:02.990 回答