我试图以线程安全的方式将一个子项添加到listview。
在单线程应用程序中,它的工作原理如下:
For i As Integer = 0 to lvMembers.Items.Count - 1
Dim lv As ListViewItem = lvMembers.Items(i)
Dim str As String = lv.Text.ToString
If str.Contains("value")
lv.SubItems.Add("OK")
lv.SubItems(0).ForeColor = Color.Green
Else
lv.SubItems.Add("FAILED")
lv.SubItems(0).ForeColor = Color.Red
End If
Next
但是,如果在另一个线程中运行,则会导致交叉线程错误。
我已经查看了使用Invoke的委托子的示例,但是我看到的所有示例都涉及更新对象的text属性,而我无法理解如何应用该概念来实际向listview添加子项
非常感谢任何帮助。
答案 0 :(得分:1)
这是一个我一遍又一遍地用于自己用途的例子。它支持传递变量。此示例中的列表视图包含1个项目和2个子项目(site,group,pagerank),因此请根据自己的需要进行修改:
代表:
Private Delegate Sub ListViewAddItem_delegate(ByVal name As String, ByVal group As String, ByVal pagerank As String)
Private Sub ListViewAddItem(ByVal name As String, ByVal group As String, ByVal pagerank As String)
If Me.ListView1.InvokeRequired Then
Dim d As New ListViewAddItem_delegate(AddressOf ListViewAddItem)
Me.ListView1.BeginInvoke(d, {name, group, pagerank})
Else
Dim lvi As New ListViewItem(name)
lvi.SubItems.Add(group)
lvi.SubItems.Add(pagerank)
ListView1.Items.AddRange(New ListViewItem() {lvi})
ListView1.Update()
End If
End Sub
如何称呼它:
Dim Name as string = "Test"
Dim Group As String = "TestGroup"
Dim PR As String = "10"
ListViewAddItem(Name, Group, PR)
我添加了“dim xxx as string”这些东西只是为了表明在调用ListViewAddItem()
时Name Group PR是字符串我注意到没有选择任何答案。帮我们一个忙,选择一个!谢谢!
答案 1 :(得分:1)
将新ListViewItem添加到ListView的线程安全简化方法:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="COUNT_KEY">235</string>
</map>
答案 2 :(得分:0)
Private delegate sub deladdtolist
Private sub addtolist
If InvokeRequired Then
del.Invoke( new deladdtolist(AddressOf addtolist))
else
For i As Integer = 0 to lvMembers.Items.Count - 1
Dim lv As ListViewItem = lvMembers.Items(i)
Dim str As String = lv.Text.ToString
If str.Contains("value")
lv.SubItems.Add("OK")
lv.SubItems(0).ForeColor = Color.Green
Else
lv.SubItems.Add("FAILED")
lv.SubItems(0).ForeColor = Color.Red
End If
Next
End If
End Sub
答案 3 :(得分:0)
一个简单的例子
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'sample data for the ListView
Dim ipsumA() As String = New String() {"Lorem", "ipsum", "dolor", "sit", _
"amet", "consectetur", "adipisicing", _
"elit", "sed", "do", "eiusmod", _
"tempor", "incididunt", "ut", "labore", _
"et", "dolore", "magna", "aliqua", "Ut", _
"enim", "ad", "minim", "veniam", "quis", _
"nostrud", "exercitation", "ullamco", _
"laboris", "nisi", "ut", "aliquip", "ex", _
"ea", "commodo", "consequat", "Duis", "aute", _
"irure", "dolor", "in", "reprehenderit", "in", _
"voluptate", "velit", "esse", "cillum", "dolore", _
"eu", "fugiat", "nulla", "pariatur", "Excepteur", _
"sint", "occaecat", "cupidatat", "non", "proident", _
"sunt", "in", "culpa", "qui", "officia", "deserunt", _
"mollit", "anim", "id", "est", "laborum"}
Dim lvi As New List(Of ListViewItem)
For Each s As String In ipsumA
Dim nLVI As New ListViewItem(s)
lvi.Add(nLVI)
Next
ListView1.Items.AddRange(lvi.ToArray)
'colorize from another thread
Dim t As New Threading.Thread(AddressOf thrd)
t.Start()
End Sub
Private Sub thrd()
If ListView1.InvokeRequired Then
ListView1.BeginInvoke(New MethodInvoker(AddressOf thrd))
Else
For i As Integer = 0 To ListView1.Items.Count - 1
Dim lv As ListViewItem = ListView1.Items(i)
Dim str As String = lv.Text.ToString
If str.ToLower.Contains("ut") Then
lv.SubItems.Add("OK")
lv.SubItems(0).ForeColor = Color.Green
Else
lv.SubItems.Add("FAILED")
lv.SubItems(0).ForeColor = Color.Red
End If
Next
End If
End Sub
End Class