在运行时获取每个项目的结果,而无需等待所有循环

时间:2013-04-09 16:34:48

标签: vb.net foreach listbox

这是我的代码:

    For Each itema In ListBox3.Items
        Dim source As String = getsource(itema)
        If source.Contains("blz blz blz")  Then
            ListBox2.Items.Add(itema & "====> here is one")

        Else
            ListBox2.Items.Add(itema)
        End If

    Next

如何在不等待vb.net中所有项目循环的情况下获取每个项目的结果? 在PHP中有flush和ob_flush来刷新内存。

2 个答案:

答案 0 :(得分:0)

如果这是Windows表单,您可以使用.Refresh()方法(Listbox2.Refresh()),或者如果您使用Windows表单,则可以使用.InvalidateVisual()方法(Listbox2.InvalidateVisual()

答案 1 :(得分:0)

如果您希望逐个添加商品,则需要"返回"并让它在每个项目之后处理消息泵,以便WPF可以更新UI。

您可以通过将代码拆分为多个Invoke()函数来进行硬编码,也可以使用Async / Await。后者看起来像下面的代码。 await Task.Yield()将控制权返回给WPF消息泵。

Async Sub MyFunction()
    For Each itema In ListBox3.Items
        Dim source As String = getsource(itema)
        If source.Contains("blz blz blz")  Then
            ListBox2.Items.Add(itema & "====> here is one")
        Else
            ListBox2.Items.Add(itema)
        Await Task.Yield()
    End If

Next