我遇到了一些問題,從非UI線程引發的事件,我不希望必須處理如果me.invokerequired每個事件處理程序添加到在Form1中的線程。從線程安全地提高事件
我確定我已經閱讀過某處如何使用委託事件(在SO上),但我無法找到它。
Public Class Form1
Private WithEvents _to As New ThreadedOperation
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
_to.start()
End Sub
Private Sub _to_SomthingHappend(ByVal result As Integer) Handles _to.SomthingHappend
TextBox.Text = result.ToString //cross thread exception here
End Sub
End Class
Public Class ThreadedOperation
Public Event SomthingHappend(ByVal result As Integer)
Private _thread As Threading.Thread
Public Sub start()
If _thread Is Nothing Then
_thread = New Threading.Thread(AddressOf Work)
End If
_thread.Start()
End Sub
Private Sub Work()
For i As Integer = 0 To 10
RaiseEvent SomthingHappend(i)
Threading.Thread.Sleep(500)
Next
End Sub
End Class
我已經與第二個建議,傳遞主機控制(我傳入一個控制對象,而不是一個表單,雖然)。謝謝 – Pondidum 2008-11-15 20:49:23