2008年9月25日 星期四

vb.net 跨執行緒使用DGV

Public Class Form1
Dim MainThread As New Threading.Thread(AddressOf MainProcess) '定義新的執行序

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Public Sub MainProcess() '執行序啟動
Dim Ii As Integer
For Ii = 0 To 1200
SetDGV(Ii, DGV)
Threading.Thread.Sleep(200)
Next
End Sub


'跨執行序呼叫元件,label 跟 button
'-------------------button
Public Sub SetDGV(ByVal [text] As String, ByVal DGV1 As DataGridView)

' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If DGV1.InvokeRequired Then
Dim d As New SetDGVCallback(AddressOf SetDGV)
DGV1.Invoke(d, New Object() {Convert.ToString([text]), DGV1})
Else
DGV1.Rows.Add()
DGV1.Rows(DGV.Rows.Count - 2).Cells(0).Value = [text]
End If
End Sub
Delegate Sub SetDGVCallback(ByVal [text] As String, ByVal DGV1 As DataGridView)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MainThread.Start()
End Sub
End Class

沒有留言:

張貼留言