简体   繁体   English

如何跟踪何时发生多个事件?

[英]How can I track when multiple events have occured?

I'm writing a plug-in for a program where I need to track when native objects are added, removed, and edited in the active document. 我正在为一个程序编写插件,该插件需要跟踪何时在活动文档中添加,删除和编辑本机对象。 The API has events that are fired when the document is edited. 该API具有在编辑文档时触发的事件。 However, the program does not track when the native objects actually change. 但是,该程序不会跟踪本地对象实际更改的时间。 Instead an object changing is treated as the object being deleted and then immediately replaced with another modified object with the same ID. 而是将对象更改视为删除的对象,然后立即将其替换为具有相同ID的另一个已修改对象。 This is done this way so the program can keep track of an undo record. 这样做是为了使程序可以跟踪撤消记录。

After some experimenting I've determined that the events are evoked as follows: 经过一些试验,我确定这些事件是由以下原因引起的:

An Object Is Added: OnAddObject Event 添加了一个对象: OnAddObject事件

An Object Is Removed: OnDeleteObject Event 删除一个对象: OnDeleteObject事件

An Object is Changed: OnReplaceObject Event->OnDeleteObject Event->OnAddObject Event 对象已更改: OnReplaceObject事件-> OnDeleteObject事件-> OnAddObject事件

Right now my plug-in is only watching the OnAdd and OnDelete events where it is adding and removing instances of my custom object to and from a collection. 现在,我的插件仅监视OnAdd和OnDelete事件,在此事件中我的自定义对象的实例在集合中添加和删除。 But this also means every time an object changes my plugin is removing an reinitializing a near identical object. 但这也意味着每次对象更改时,我的插件都将删除重新初始化几乎相同的对象。 I'd rather just know that the document object has changed so my custom object can be refreshed rather then completely reinstantiated. 我只想知道文档对象已更改,因此可以刷新我的自定义对象,然后再重新实例化。

How can my methods that are subscribed to the OnDelete and OnAdd events tell that the object is not really being added or deleted but is being replaced because it has changed? 预订OnDelete和OnAdd事件的方法如何得知该对象并未真正添加或删除,而是因为已更改而被替换了?

跟踪文档ID。

That's a bit of a tough one because of the bad event structure of the original program. 由于原始程序的不良事件结构,这有点困难。 Perhaps you could put the id of the item that's being removed in a dictionary together with the current time. 也许您可以将要删除的项目的ID与当前时间一起放在字典中。

That way you could create separate thread that checks the content of the dictionary (when it contains items). 这样,您可以创建单独的线程来检查字典的内容(当它包含项目时)。 Give each item a few milliseconds (compare the DateTime from the dict.) for the Add event to kick in and so decide if it is a regular delete or a replace. 给每个项目几毫秒的时间(比较字典中的DateTime),以使Add事件生效,然后确定它是常规删除还是替换。

It's a bit inefficient because you'll have to slowdown your delete a few millisec. 这有点效率低下,因为您必须将删除速度减慢几毫秒。 But I can't think of a better way. 但是我想不出更好的方法。

I suggest introducing "lock" variables (eg bools) 我建议引入“锁定”变量(例如布尔)

When the Replaced event fires, set them to lock 当替换事件触发时,将其设置为锁定

In Added and removed check the lock variables. 在添加和删除中,检查锁变量。 If they are set to lock, set them to unlock and return. 如果将它们设置为锁定,则将它们设置为解锁并返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM