在VB.NET中连接到Application.DoEvents方法

时间:2022-12-29 00:01:51

I am not very familiar with VB.NET so I don't know if this is possible...

我对VB.NET不是很熟悉所以我不知道这是否可能......

Some code I am working on is setting the properties of a very large class. In the setter of each property an event is raised to indicate that the class data has changed. An event handler then serializes the instance of the class to a database.

我正在处理的一些代码是设置一个非常大的类的属性。在每个属性的setter中,引发一个事件以指示类数据已更改。然后,事件处理程序将类的实例序列化为数据库。

Obviously I do not want this to happen after each property is set, so I need to either have a delay before saving, or something else.

显然我不希望在每个属性设置后发生这种情况,所以我需要在保存之前有一个延迟,或者别的什么。

I am keeping a large list of instances in a cache implementation already, so one option would be to only process the cache every now and then and save all unsaved instances in cache to the database.

我已经在缓存实现中保留了大量实例,因此一个选项是不时地处理缓存并将缓存中的所有未保存实例保存到数据库中。

So I tried to see if I could derive a class from Application (and override DoEvents), but it is NotInheritable, so no luck there.

所以我试着看看我是否可以从Application派生一个类(并覆盖DoEvents),但它是NotInheritable,所以那里没有运气。

Any ideas?

3 个解决方案

#1


It isn't entirely clear whether you're talking about the windows message pump, or .NET events. Either way, there are a few common approaches:

您是在谈论Windows消息泵还是.NET事件还不完全清楚。无论哪种方式,有一些常见的方法:

  • change the publisher so that it only raises a single event when everything is done; for example, with data-binding to BindingList<T> you can set RaiseListChangedEvents to false while doing a big update, then true afterwards and call the reset method
  • 更改发布者,以便在完成所有操作后只会引发一个事件;例如,通过数据绑定到BindingList ,您可以在执行大更新时将RaiseListChangedEvents设置为false,然后再将其设置为true并调用reset方法

  • tell the target to disable things like drawing for a duration; for example using a BeginEdit()/ EndEdit() pair of methods
  • 告诉目标禁止画画一段时间;例如,使用BeginEdit()/ EndEdit()方法对

  • if neither is possible: handle the events, but don't do anything immediately; only do something once you believe nothing else is coming; for example by adding a short delay before you do your funky stuff
  • 如果两者都不可能:处理事件,但不要立即做任何事情;只有在你相信没有其他事情要来的时候才会做某事例如,在做你的时髦之前添加一个短暂的延迟

An example of the last is here: Prevent events from firing multiple times from single action

最后一个例子是:防止事件从单个动作多次触发

#2


Add an extra field to your EventArgs class that says "handled". Then, every handler checks this field, and continues only if it is false. Then, once it's done, it sets the field to True.

在EventArgs类中添加一个额外的字段,表示“已处理”。然后,每个处理程序都会检查此字段,并仅在其为false时继续。然后,一旦完成,它将字段设置为True。

#3


There is no "event queue".

没有“事件队列”。

Are you asking about this because of a problem you've seen, or because of a concern that you have? If you're asking about a problem, then please edit your question to include details about the problem. If you're asking about a concern, then please edit the question to include some information about what you're concerned about.

你是在问这个问题,因为你看到了一个问题,还是因为你有一个问题?如果您询问问题,请编辑您的问题以包含有关问题的详细信息。如果您正在询问问题,请编辑问题以包含您关注的一些信息。

#1


It isn't entirely clear whether you're talking about the windows message pump, or .NET events. Either way, there are a few common approaches:

您是在谈论Windows消息泵还是.NET事件还不完全清楚。无论哪种方式,有一些常见的方法:

  • change the publisher so that it only raises a single event when everything is done; for example, with data-binding to BindingList<T> you can set RaiseListChangedEvents to false while doing a big update, then true afterwards and call the reset method
  • 更改发布者,以便在完成所有操作后只会引发一个事件;例如,通过数据绑定到BindingList ,您可以在执行大更新时将RaiseListChangedEvents设置为false,然后再将其设置为true并调用reset方法

  • tell the target to disable things like drawing for a duration; for example using a BeginEdit()/ EndEdit() pair of methods
  • 告诉目标禁止画画一段时间;例如,使用BeginEdit()/ EndEdit()方法对

  • if neither is possible: handle the events, but don't do anything immediately; only do something once you believe nothing else is coming; for example by adding a short delay before you do your funky stuff
  • 如果两者都不可能:处理事件,但不要立即做任何事情;只有在你相信没有其他事情要来的时候才会做某事例如,在做你的时髦之前添加一个短暂的延迟

An example of the last is here: Prevent events from firing multiple times from single action

最后一个例子是:防止事件从单个动作多次触发

#2


Add an extra field to your EventArgs class that says "handled". Then, every handler checks this field, and continues only if it is false. Then, once it's done, it sets the field to True.

在EventArgs类中添加一个额外的字段,表示“已处理”。然后,每个处理程序都会检查此字段,并仅在其为false时继续。然后,一旦完成,它将字段设置为True。

#3


There is no "event queue".

没有“事件队列”。

Are you asking about this because of a problem you've seen, or because of a concern that you have? If you're asking about a problem, then please edit your question to include details about the problem. If you're asking about a concern, then please edit the question to include some information about what you're concerned about.

你是在问这个问题,因为你看到了一个问题,还是因为你有一个问题?如果您询问问题,请编辑您的问题以包含有关问题的详细信息。如果您正在询问问题,请编辑问题以包含您关注的一些信息。