Use the PublishSubscribe design pattern to reach the target data synchronization (2)

zhaozj2021-02-16  52

During registration processing, Subscriber is assigned a unique tag to identify Subscriber in the Event Channel. Event Channel also uses this tag indexing those Subscriber. Although the sample application uses a messy pulse interference of the marker target, I recommend using another method to generate a unique identifier (for example, a GUID) in your own program.

Use the directory menu to establish a 3 to 4 FRMLIST window instances. Use the new directory menu option to create a 3 or 4 instance of FRMLIST, then select an entry in one window, double-click this entry to enter the editable mode, type a new name. The form creates an event that describes data updates and sends through Event Channel.

Private sub lvwlist_afterlabeledit (Byval Sender AS _ Object,

Byval e as _ system.windows.forms.labelediteventArgs_

Handles lvwlist.afterlabeledit

DIM Objevent As ClsdataEvent

IF E.Label <> "" ""

Objevent = new clsdataEvent ()

With objevent

.Name = "namechanged"

.Value = E.Label

.Extradata = lvwlist.items (E.Item) .tag

.Origin = me

End with

Mybase.Eventchannel.publishevent (Objevent)

END IF

End Sub

The form of the event confirms that it is the founder of the event; and set the CLSDataEvent class's origin property to itself. This can block other excess events to ensure that the events received by the sender have been released. Finally, the issuer of the event notes the changes in the data.

All Subscribers will receive an event. In EventApp, each FRMLIST instance will receive an event and change the change in the changing in each ListView:

Public overrides sub Eventraise (_ byval newevent as _

EVENTAPP.CLSEVENT)

Dim ObjlistItem As ListViewItem

Select Case NewEvent.name

Case "NameChanged"

For Each ObjlistItem in lvwlist.items

IF ObjlistItem.tag = newevent.extradata Then

ObjlistItem.text = newevent.value

END IF

NEXT

Case Else

'Handle Other Events

End SELECT

End Sub

This type of event system will bring you great flexibility. You can make any one in the app

Object publishing events, only those objects to this to update. You can also change Event

The implementation of the CHANNEL class, such as using the web service or message system (such as MS)

Message Queue or IBM MQ series), with HTTP co-access events.

转载请注明原文地址:https://www.9cbs.com/read-23473.html

New Post(0)