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关键字。 –