- Код: Выделить всё
Public Class Main
Private Core As Core
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Core = New Core
End Sub
Private Sub btn_Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Start.Click
Core.Thread_Manager()
End Sub
Public Delegate Sub DlgList(ByVal Index As Integer, ByVal Result As String)
Public DelegateList As New DlgList(AddressOf List)
Public Sub List(ByVal Index As Integer, ByVal Result As String)
If Not Result Is Nothing Then
lv_List.Items(Index).SubItems(2).Text = Result
Else
lv_List.Items(Index).SubItems(2).Text = "not found"
End If
End Sub
Public Delegate Function DlgGetList()
Public DelegateGetList As New DlgGetList(AddressOf GetList)
Public Function GetList()
Dim ListViewItemCollection(lv_List.Items.Count - 1) As ListViewItem
lv_List.Items.CopyTo(ListViewItemCollection, 0)
Return ListViewItemCollection
End Function
End Class
- Код: Выделить всё
Class Core
Public Form As Main = Main
Public Options As New Main_Class.Options
Public Sub Thread_Manager()
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Process_Work), Nothing)
End Sub
Private Sub Process_Work()
Dim ListViewItemCollection() As ListViewItem = Nothing
Thread.CurrentThread.IsBackground = True
ListViewItemCollection = Form.Invoke(Form.DelegateGetList, Nothing)
For i = 0 To ListViewItemCollection.Count - 1
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Thread_Work), ListViewItemCollection(i))
Next
End Sub
Private Sub Thread_Work(ByVal ListViewItem As ListViewItem)
Dim Result As String = Nothing
For j = 0 To UBound(Work_Module.Work_Type)
If Work_Module.Work_Type(j).Name = ListViewItem.Group.Name Then
Result = Work_Module.Request(ListViewItem.SubItems(0).Text, Work_Module.Work_Type(j).Site(0))
Form.Invoke(Form.DelegateList, New Object() {ListViewItem.Index, Result})
Exit For
End If
Next
End Sub
End Class
И в ходе работы на втором инвоке вылетатет - "Cross-thread operation not valid: Control 'lv_List' accessed from a thread other than the thread it was created on."
Как сделать, чтобы работало?