如何使用Task.Factory.StartNew更新UI线程?

时间:2014-07-23 17:12:33

标签: asp.net vb.net task-parallel-library

我有以下代码,但它最初不会更新UI线程。我做错了吗?

Protected Sub Copy_OnClick(sender As Object, e As EventArgs)

    StatusLabel.Text = "Processing..." 'This doesn't happen
    Copy.Enabled = False 'This doesn't happen

    Dim copyTask As Task = Task.Factory.StartNew(Sub() Copy())

    copyTask.Wait()

    If (copyTask.IsCompleted) Then
        Copy.Enabled = True
        StatusLabel.Text = "Done" 'This happens     
    End If

End Sub

1 个答案:

答案 0 :(得分:2)

是的,您正在等待UI线程。你为什么要开始一项任务然后立即等待呢?这在概念上没有意义。

使用其中一种标准技术来解锁UI线程。