使用观察者模式的奇怪行为

发布于 2024-10-12 01:45:33 字数 1695 浏览 2 评论 0原文

好的,所以我有一个读取另一个进程内存的应用程序。我最初有多个扫描线程来扫描我需要阅读的各个区域。这是处理器密集型的,所以我决定采用观察者模式。一切都很好,除了我的行为很奇怪。

这是正在发生的事情

我有 2 个雷达(覆盖和映射)两者都有一个附加到内存扫描仪的观察者类,并在新的怪物列表上收到通知。

所以我打开雷达 1(映射),它将其观察者附加到扫描仪并等待生物列表更新通知

打开雷达 2(覆盖)。同样的事情发生了,并且附加了另一个观察者。

到目前为止一切都很好 现在列表中有小怪的属性,其中之一是 IsFilteredOut。该属性在雷达代码收到列表后设置。

现在奇怪的行为是,无论我做什么,打开的第二个雷达都会改变两个雷达列表中生物的所有属性。就好像我正在通过 ref 传递列表,但我没有。实际上,每次传递列表时,我都会创建 moblist 类的一个新实例。

这是通知代码。正如您所看到的,我每次都会创建 moblist 类的一个新实例。

Private Sub NotifyMobListUpdated(ByVal Mobs As List(Of MobData))
    If Mobs IsNot Nothing Then
        For Each w As Watcher In _watchers
            If w.Type And WatcherTypes.MobList = WatcherTypes.MobList OrElse w.Type And WatcherTypes.All = WatcherTypes.All Then
                w.MobListUpdated(New MobList(Mobs))
            End If
        Next
    End If
End Sub

这是在 Watcher 类中处理它的地方。

''' <summary>
''' IWatcher MoblistUpdated Implementation
''' </summary>
''' <param name="Mobs">The Updated mob list</param>
''' <remarks></remarks>
Public Sub MobListUpdated(ByVal Mobs As MobList) Implements IWatcher.MobListUpdated
    Try
        PostNewMobList(Mobs)
    Catch ex As Exception
    End Try
End Sub

Public Sub PostNewMobList(ByVal Mobs As MobList)
    _sync.Post(New SendOrPostCallback(AddressOf OnNewMobList), Mobs)
End Sub

Private Sub OnNewMobList(ByVal state As Object)
    Dim mobs As MobList = TryCast(state, MobList)
    Try
        If mobs IsNot Nothing Then
            RaiseEvent NewMobList(mobs)
        End If
    Catch ex As Exception
    End Try
End Sub

这个错误让我发疯,任何帮助将不胜感激。

谢谢

Ok, so I have an application that reads another processes memory. I initially had multiple scanning threads for the various areas I needed to read. This was processor intensive so I decided to go with the observer pattern. All was well except that I am having a weird behavior.

Here is what is happening

I have 2 radars (overlay and mapped) Both have a watcher class that attaches to the memory scanner and is notified on a new list of mobs.

so I open radar 1 (mapped) it attaches it's watcher to the scanner and waits for mob list update notifications

Open radar 2 (overlay). same thing happens and another watcher is attached.

all is well and good so far
Now there are properies on the mobs in the list, one of which is IsFilteredOut. This property is set in the radar code after it receives the list.

Now the weird behavior is that no matter what I do, the second radar to be opened changes all the properties of the mobs in the list of both radars. It is as if I am passing the list by ref, but I am not. I actually create a new instance of the moblist class every time I pass the list.

Here is the notify code. As you can see I create a new instance of the moblist class each pass.

Private Sub NotifyMobListUpdated(ByVal Mobs As List(Of MobData))
    If Mobs IsNot Nothing Then
        For Each w As Watcher In _watchers
            If w.Type And WatcherTypes.MobList = WatcherTypes.MobList OrElse w.Type And WatcherTypes.All = WatcherTypes.All Then
                w.MobListUpdated(New MobList(Mobs))
            End If
        Next
    End If
End Sub

This is where it is handled in the Watcher class

''' <summary>
''' IWatcher MoblistUpdated Implementation
''' </summary>
''' <param name="Mobs">The Updated mob list</param>
''' <remarks></remarks>
Public Sub MobListUpdated(ByVal Mobs As MobList) Implements IWatcher.MobListUpdated
    Try
        PostNewMobList(Mobs)
    Catch ex As Exception
    End Try
End Sub

Public Sub PostNewMobList(ByVal Mobs As MobList)
    _sync.Post(New SendOrPostCallback(AddressOf OnNewMobList), Mobs)
End Sub

Private Sub OnNewMobList(ByVal state As Object)
    Dim mobs As MobList = TryCast(state, MobList)
    Try
        If mobs IsNot Nothing Then
            RaiseEvent NewMobList(mobs)
        End If
    Catch ex As Exception
    End Try
End Sub

This error is driving me nuts and any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2024-10-19 01:45:33

实际上,每次传递列表时,我都会创建 moblist 类的一个新实例。

这只阻止列表更改,而不是列表元素。您还必须克隆元素对象。我对雷达和生物一无所知,您可能需要考虑使用“发送”而不是“邮寄”。

I actually create a new instance of the moblist class every time I pass the list.

Which only prevents the list from changing, not the list elements. You'd have to clone the element objects as well. I don't have a clue with radars and mobs do, you might want to consider using Send instead of Post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文