vb.net - 调用 Invoke 时出现空白 ListView
问题描述
listView
归类所有Form1
。一个单独的类传输中的子程序anotherThread
由一个线程中的子程序启动Form1
。Form1
拥有另一个公共子例程addItemsListView
,它使用Invoke
. 当transmission.anotherThread
调用addItemsListView
时,子例程运行,但listView
保持空白。
在每个类中都尝试过委托、调用等,但同样的问题。
Class Form1
Property myTransmission = New transmission
Private Sub aSubRoutine() Handles MyBase.Load
Dim t As New Threading.Thread(
Sub()
myTransmission.anotherThread()
End Sub
)
t.Start()
End Sub
Public Sub addItemsListView(ByVal items As String())
If listView.InvokeRequired Then
listView.Invoke(Sub() addItemsListView(items))
Else
For each item In Items
listView.Items.Add(item)
Next
End If
End Sub
End Class
Class transmission
Public Sub anotherThread()
Form1.addItemsListView(New String() {"abc", "def"})
End Sub
End Class
所以我希望“abc”和“def”在listView
但它仍然完全空白。但是,如果我单步执行代码,一切似乎都运行顺利。
解决方案
你不是在和你现有的表格说话。将其作为参考传递:
Public Sub anotherThread(inForm As Form1)
inForm.addItemsListView(New String() {"abc", "def"})
End Sub
然后在您调用它时包含您的表单:
myTransmission.anotherThread(Me)
推荐阅读
- rxjs - 哪种情况是“点击”运算符的适当用法?
- java - 从 Jar 依赖项中运行应用程序
- package.json - 删除重复包以及 yarn install --pure-lockfile 和 yarn install freeze-lockfile 之间的区别
- matlab - 在 MATLAB 中对块对角矩阵的高效运算
- python - python中的正则表达式(123/4)
- regex - 修改正则表达式以在带有或不带有 http 前缀的字符串中查找 URL
- ios - Xcode - 试图在 iPhone 上运行 - 签名不起作用
- sql - WCF 事务回滚,同时使用两个服务更新两个不同的数据库 - 一个是 SQL 数据,另一个是 Couchbase 数据
- ios - iOS 11 UITableView performBatchUpdates 完成闭包未被调用
- node.js - 读取word文件,填写一些文本并在nodejs中创建一个文件word