开发者

Weird behavior using the Observer pattern

开发者 https://www.devze.com 2023-02-04 10:12 出处:网络
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

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)
    Cat开发者_高级运维ch 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消