2011年3月10日 星期四

多線程處理事件

本範例,來自CSDN的詢問;我稍調整
---
---
Imports System.Threading
Public Class Form1
Dim N, m As Integer
Dim P As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("狀態")
ListView1.Columns.Add("路徑")
ListView1.Items.Add("未處理")
ListView1.Items.Add("未處理")
ListView1.Items.Add("未處理")
ListView1.Items(0).SubItems.Add("C:\a.jpg")
ListView1.Items(1).SubItems.Add("C:\b.jpg")
ListView1.Items(2).SubItems.Add("C:\c.jpg")


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
m = ListView1.Items.Count

Dim td As Thread = Nothing
td = New Thread(New ThreadStart(AddressOf do_work))
td.Start()

End Sub

Delegate Sub addInvo(ByVal i As Integer)

Dim td1() As Thread
Sub do_work()
ReDim td1(m - 1)
For i = 0 To m - 1

td1(i) = New Thread(New ParameterizedThreadStart(AddressOf R_LV))
td1(i).Start(i)
Next
End Sub

Sub R_LV(ByVal i As Integer)
If ListView1.InvokeRequired = False Then
If ListView1.Items(i).SubItems(0).Text = "未處理" Then
P = ListView1.Items(i).SubItems(1).Text

'處理事件
If i >= 0 Then
ListView1.Items(i).SubItems(0).Text = "已完成"
MsgBox(P)
End If

ElseIf ListView1.Items(i).SubItems(0).Text = "已完成" Then

End If
Else
Dim del As New addInvo(AddressOf R_LV)
ListView1.BeginInvoke(del, New Object() {i})
End If
End Sub
End Class

沒有留言:

張貼留言