VB.Net中BackgroundWorker的后期绑定问题

时间:2017-05-29 14:58:42

标签: vb.net backgroundworker late-binding

我正在运行BackgroundWorker,并希望报告其进度。在下面的示例中,我创建了一个测试列表,然后BackgroundWorker将遍历该测试列表。问题出在'sender.ReportProgress(i)'行。如果我有Option Strict,则由于Late Binding问题,它不喜欢我使用'i'。有没有其他方法来编码并避免这个问题?

<application-bnd>
    <security-role name="worklightadmin">
        <user name="admin"/>
    </security-role>
        <security-role name="worklightdeployer">
        <user name="deployer"/>
    </security-role>
        <security-role name="worklightmonitor">
        <user name="monitor"/>
    </security-role>
        <security-role name="worklightoperator">
        <user name="operator"/>
    </security-role>
</application-bnd>

2 个答案:

答案 0 :(得分:1)

CType(sender, BackgroundWorker).ReportProgress(i)

答案 1 :(得分:1)

此外,如果您想对其执行多个操作,请创建一个本地引用变量,如下所示:

Private Sub bgw_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs)

    Dim bgw As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)

    ' ... now you can use "bgw" multiple times below instead of casting each time ...

    For i = 0 To MyList.Count -1
        Label1.Text = MyList(i)
        bgw.ReportProgress(i)
        bgw.SomethingElse()
        bgw.MoreStuff()
        System.Threading.Thread.Sleep(200)
        Label1.Refresh()
    Next
End Sub

显然,在你的情况下这不是必要的,只是一个FYI ......