0
我創建了繼承類的對象。其中一個事件是在列表中發起更改。我試着寫了一個例子,但我發現放下我的邏輯要容易得多。 「發生事件」的意思是RaiseEvent
。這也是我第一次使用自定義事件和可繼承的類。處理來自繼承類的事件
- (在自己的線程)
- 繼承類運行MyBase.New()
- 基地班開始監聽傳入的請求
- 如果收到請求,並有效創建繼承類的對象,引發基類的事件,由繼承類處理(此事件可能會修改列表)
- 如果列表發生更改,則會引發繼承類的事件並由GUI處理
- 基類返回等待新請求
我試圖避免從繼承類編輯GUI。問題是事件永遠不會觸發(它忽略了第5步中的RaiseEvent,並且GUI從未獲得事件)。可能單獨的線程是一個問題?
步驟5的實施例:
Private Sub RequestReceived(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.RequestReceived
'RequestReceived is from the Base Class
Dim requestType As String = sender
If requestType = "Type1" Then
RequestIsVariantOne(sender)
Else Then
'Handle other variants similarly
End If
End Sub
Private Sub RequestIsVariantOne(ByVal sender As Object)
'Conditional statements go here that determine whether or not to edit the list
'The statements will exit the Sub if it the list should not be edited.
'If Sub hasn't exited yet, now we edit the list.
ThatList.Add(sender)
'ListChanged is from the Inherited class
RaiseEvent ListChanged(ThatList, Nothing)
End Sub
有關OOP(繼承,事件)的概念不與多線程連接,它講述了數據結構如何組織的想法。正如我認爲的,這是沿着多線程問題調查您的問題的錯誤方法。您是否介意發佈一些代碼來演示step5事件如何引發? –
所以你說的只是我的問題與線程無關?當然,我會發布一些東西。 –
有了類聽自己的事件就沒有什麼意義了。改用虛擬方法,使用Overridable和Overrides關鍵字。 –