这是一个代码示例,您需要在表单上ListBox
调用list_of_items
,以重现我遇到的问题:
Imports System.Threading.Tasks
Public Class Form1
Dim _dt As DataTable
Private Sub Form1_Load() Handles MyBase.Load
_dt = New DataTable
With _dt.Columns
.Add("key")
.Add("value")
End With
With list_of_items
.ValueMember = "key"
.DisplayMember = "value"
.DataSource = _dt
End With
Dim addItemsTask As New Task(AddressOf AddThreeItems)
addItemsTask.Start() 'does not add anything when done
'AddThreeItems() #doing this instead works!
End Sub
Private Sub AddThreeItems()
Threading.Thread.Sleep(2000)
With _dt.Rows
.Add({"1", "One"})
.Add({"2", "Two"})
.Add({"3", "Three"})
End With
Me.Invoke(Sub() Me.Text = "Separate thread is done")
End Sub
End Class
问题是行确实是物理添加的,所以DataTable.Rows.Count
增加了,但视觉上没有任何反应。我试过打电话Refresh
,重置和DataSource
返回Nothing
- 它没有帮助。如果我将其切换为单线程处理,则可以使用图示的方法很好地添加行。可能是什么问题?