vb.net中的事件处理 [英] Event Handling in vb.net

查看:78
本文介绍了vb.net中的事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的VB都不包括vb6,因为一个事件可能会让b / b
没有中断。本身。


例如,如果你有一个包含msgbox的计时器事件,那么你只需要获得一条消息。


但是在vb.net中你会得到连续的消息(甚至设置系统

模态属性)。


首先,这两个假设是正确的,如果那么什么是停止事件中断自己的批准

方式?


其次,对于VB.net中的所有事件都是如此,我是否必须保护

我自己在任何情况下?


TIA

解决方案

杰克,

我所有的VB中,包括vb6在内的所有VB都是因为一个事件无法中断。本身。
任何Windows事件都可以中断本身就是从VB1开始调用DoEvents。


在VB.NET和VB中都是如此。 VB6 MsgBox& MessageBox.Show调用相当于

" DoEvents",我怀疑确保底层表单正确显示

(处理Paint事件)。


当你打电话给DoEvents或它的等价物时,任何事件都可以再次发生,

除了我相信Paint和另外一个。

首先,是这两个假设是正确的


,如果是这样,那么批准停止事件中断的方式是什么?
如果我在处理事件时调用DoEvents,我通常在

事件中设置一个标志,表示它已经被处理。


Something喜欢:


Private Sub Timer1_Tick(ByVal发送者As System.Object,ByVal e As

System.EventArgs)处理Timer1.Tick

静态被处理为布尔值

如果被处理,则退出Sub

被处理=真实

''在这里工作

被处理=错误

结束子

其次,对于VB.net中的所有事件都是如此,当我在任何情况下我必须自己保护

你只需保护自己,无论DoEvents是明确地

还是隐式调用。


FWIW有些事件不是Windows事件,这些事件不是通过DoEvents关键字处理的
。 Windows窗体应用程序有一条消息

pump(Application.Run)导致Windows消息中的Windows消息

队列转换为事件,每个事件都在内部处理,除非你用
调用DoEvents导致消息队列中的下一条消息被转换成各自的事件。因此,如果您明确地调用DoEvents

或隐式地可以再次调用事件。如果在Timer Tick,ToolBar ButtonClick,

Button Click和MenuItem Click事件中调用DoEvents,这可能是

问题的根源。


希望这有帮助

Jay

" Jack Russell" < JA *** @ norubbish.tpg.com.au>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...我所有的VB都没有,包括vb6是一个事件无法中断例如,如果您有一个包含msgbox的计时器事件,那么您只会收到一条消息。

但是在vb.net中,您会收到不断的消息(甚至设置系统
模态属性)。

首先,这两个假设是正确的,如果是这样的话,批准停止事件的方式是什么?
其次,对于VB.net中的所有事件都是如此,我在任何情况下都必须保护自己吗?

TIA


Doh!

根据事件处理的性质,在Timer.Tick中,Try / Finally可能对
有益事件
私人小组Timer1_Tick(BYVAL发件人为System.Object的,BYVALË作为
System.EventArgs)把手Timer1.Tick
静态beingHandled由于布尔
如果被处理,则退出Sub
尝试被处理=真正
''在这里工作
最后beingHandled = False
结束尝试结束Sub


因为Last将确保关闭beingHandled标志,

如果你正在使用特别重要全球异常处理程序。


希望这有帮助

Jay


Jay B. Harlow [MVP - 前景] QUOT; < JA ************ @ msn.com>在消息中写道

新闻:OM ************* @ TK2MSFTNGP10.phx.gbl ...杰克,

我的所有人都没有VB直到并包括vb6是一个事件可能不会中断。本身。


任何Windows事件都可以中断。从VB1开始调用DoEvents本身。

在VB.NET和VB中都是如此。 VB6 MsgBox& MessageBox.Show调用相当于
的DoEvents,我怀疑确保底层表单正确显示
(处理Paint事件)。

当你调用DoEvents时或者相当于任何事件可以发生在第二个时间,除了我相信Paint和另外一个。

首先,这两个假设是正确的



如果是这样,那么停止事件中断的批准方式是什么?


如果我在处理事件时调用DoEvents,我通常在
事件中设置一个标志,表示它已被处理。

私有子Timer1_Tick(ByVal发送者为System.Object,ByVal e As
System.EventArgs)处理Timer1.Tick
静态被处理为布尔值
如果被处理,则退出Sub
被处理=真正
''在这里工作被处理=错误
结束Sub

其次是VB.net中的所有事件都是如此,当我在任何情况下我是否必须保护自己?


只有在明确地或隐式调用DoEvents的情况下,您才需要保护自己。

FWIW有些事件不是Windows事件,这些事件不是
通过DoEvents关键字处理。 Windows窗体应用程序有一个消息泵(Application.Run),它将Windows消息队列中的Windows消息转换为事件,每个事件都在内部处理,
除非您调用DoEvents与导致消息队列中的下一条消息转换为其各自的事件。因此,如果您显式或隐式地调用DoEvents,则可以再次引发事件。如果在Timer Tick,ToolBar
ButtonClick,Button Click和MenuItem Click事件中调用DoEvents,这可能会成为问题的根源。

希望这会有所帮助
Jay

Jack Russell < JA *** @ norubbish.tpg.com.au>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

我所有的VB都没有出现包括vb6在内是一个事件可能不会中断例如,如果您有一个包含msgbox的计时器事件,那么您只会收到一条消息。

但是在vb.net中,您会收到不断的消息(甚至设置系统
模态属性)。

首先,这两个假设是正确的,如果是这样的话,批准停止事件的方式是什么?
其次,对于VB.net中的所有事件都是如此,我在任何情况下都必须保护自己吗?

TIA



Jay,


我非常感兴趣地阅读了您的回复,因为我正在开发一个应用程序

需要一个计时器。你在谈论什么,即重新输入定时器处理程序,需要两个线程,对吗?我不是说

程序员已经要求多个线程,但不知何故,在

封面下,Windows正在使用多个线程。如果那不是这样的话,你不得不做一些事情来设置你的被遗忘的旗帜

原子?否则两个线程都可以在任一线程设置之前获取并检查isHandled

标志。你怎么在.Net中做相当于

的关键部分?


" Jay B.哈洛[MVP - Outlook]" < JA ************ @ msn.com>在消息中写道

新闻:OM ************* @ TK2MSFTNGP10.phx.gbl ...

杰克,

我的所有VB中包括vb6在内的所有未完成的事件都是
不能中断本身。任何Windows事件都可以中断从VB1开始调用DoEvents本身。

在VB.NET和VB中都是如此。 VB6 MsgBox& MessageBox.Show调用相当于
的DoEvents,我怀疑确保底层表单正确显示
(处理Paint事件)。

当你调用DoEvents时或者相当于任何事件可以发生第二次



时间,除了我相信Paint和另外一个。

首先,这两个假设是对的否

如果是这样,那么停止事件中断的批准方式是什么?


如果我在处理事件时调用DoEvents,我通常在
事件中设置一个标志,表示它已被处理。

私有子Timer1_Tick(ByVal发送者为System.Object,ByVal e As
System.EventArgs)处理Timer1.Tick
静态被处理为布尔值
如果被处理,则退出Sub
被处理=真正
''在这里工作是汉德尔ed = False
End Sub

其次是VB.net中的所有事件都是如此,我在任何情况下都必须保护自己吗?


只有在明确地或隐式调用DoEvents的情况下,您才需要保护自己。

FWIW有些事件不是Windows事件,这些事件是不通过DoEvents关键字处理。 Windows窗体应用程序有一个



消息泵(Application.Run),它导致Windows消息队列中的Windows消息转换为事件,每个事件都在内部处理,除非
您调用DoEvents会导致消息队列中的下一条消息被转换为其各自的事件。因此,如果您明确地调用DoEvents,或者隐式地可以再次调用事件。如果在Timer Tick,ToolBar ButtonClick,
Button Click和MenuItem Click事件中调用DoEvents,这可能是一个源
的问题。

希望这会有所帮助
Jay

Jack Russell < JA *** @ norubbish.tpg.com.au>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

我所有的VB都没有出现包括vb6在内是一个事件可能是
而不是中断例如,如果您有一个包含msgbox的计时器事件,那么您只会收到一条消息。

但是在vb.net中,您会收到不断的消息(甚至设置系统
模态属性)。

首先,这两个假设是正确的,如果是这样的话,批准停止事件的方式是什么?
其次,对于VB.net中的所有事件都是如此,我在任何情况下都必须保护自己吗?

TIA



My unstanding of all VB up to and including vb6 is that an event could
not "interrupt" itself.

For instance if you had a timer event containing a msgbox then you would
only get one message.

However in vb.net you get continual messages (even setting the system
modal property).

Firstly, are these two assumptions right and if so what is the approved
way of stopping the event interrupting itself?

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?

TIA

解决方案

Jack,

My unstanding of all VB up to and including vb6 is that an event could not
"interrupt" itself. Any Windows Event can "interrupt" itself by calling DoEvents since VB1.

In both VB.NET & VB6 MsgBox & MessageBox.Show calls the equivalent of
"DoEvents", I suspect to ensure the underlying form is properly displayed
(the Paint event is handled).

When you call DoEvents or its equivalent any event can occur a second time,
except I believe Paint and one other.
Firstly, are these two assumptions right No
and if so what is the approved way of stopping the event interrupting
itself? If I call DoEvents while processing an Event, I normally set a flag in the
event that says it is already being handled.

Something like:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static beingHandled As Boolean
If beingHandled Then Exit Sub
beingHandled = True
'' do work here
beingHandled = False
End Sub
Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event? You only have to protect yourself in any event that DoEvents is explicitly
or implicitly called.

FWIW there are events that are not Windows Events, these events are not
handled via the DoEvents keyword. Windows Forms applications have a message
pump (Application.Run) that causes Windows Messages in the Windows Message
Queue to be converted into Events, each event is handled inturn, unless you
call DoEvents with causes the next message in the message queue to be
converted into its respective Event. Hence if you call DoEvents explicitly
or implicitly an event can be raised a second time. This can be a source of
problems if DoEvents is called with in Timer Tick, ToolBar ButtonClick,
Button Click and MenuItem Click events.

Hope this helps
Jay
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... My unstanding of all VB up to and including vb6 is that an event could not
"interrupt" itself.

For instance if you had a timer event containing a msgbox then you would
only get one message.

However in vb.net you get continual messages (even setting the system
modal property).

Firstly, are these two assumptions right and if so what is the approved
way of stopping the event interrupting itself?

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?

TIA



Doh!
Depending on the nature of the event processing a Try/Finally might be
beneficial in the Timer.Tick event

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static beingHandled As Boolean
If beingHandled Then Exit Sub Try beingHandled = True
'' do work here Finally beingHandled = False End Try End Sub
As the Finally will ensure that the beingHandled flag is turned off,
especially important if you are using a Global Exception handler.

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OM*************@TK2MSFTNGP10.phx.gbl... Jack,

My unstanding of all VB up to and including vb6 is that an event could
not "interrupt" itself.


Any Windows Event can "interrupt" itself by calling DoEvents since VB1.

In both VB.NET & VB6 MsgBox & MessageBox.Show calls the equivalent of
"DoEvents", I suspect to ensure the underlying form is properly displayed
(the Paint event is handled).

When you call DoEvents or its equivalent any event can occur a second
time, except I believe Paint and one other.

Firstly, are these two assumptions right


No

and if so what is the approved way of stopping the event interrupting
itself?


If I call DoEvents while processing an Event, I normally set a flag in the
event that says it is already being handled.

Something like:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static beingHandled As Boolean
If beingHandled Then Exit Sub
beingHandled = True
'' do work here
beingHandled = False
End Sub

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?


You only have to protect yourself in any event that DoEvents is explicitly
or implicitly called.

FWIW there are events that are not Windows Events, these events are not
handled via the DoEvents keyword. Windows Forms applications have a
message pump (Application.Run) that causes Windows Messages in the Windows
Message Queue to be converted into Events, each event is handled inturn,
unless you call DoEvents with causes the next message in the message queue
to be converted into its respective Event. Hence if you call DoEvents
explicitly or implicitly an event can be raised a second time. This can be
a source of problems if DoEvents is called with in Timer Tick, ToolBar
ButtonClick, Button Click and MenuItem Click events.

Hope this helps
Jay
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

My unstanding of all VB up to and including vb6 is that an event could
not "interrupt" itself.

For instance if you had a timer event containing a msgbox then you would
only get one message.

However in vb.net you get continual messages (even setting the system
modal property).

Firstly, are these two assumptions right and if so what is the approved
way of stopping the event interrupting itself?

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?

TIA




Jay,

I read your response with great interest because I am working on an app
which will need a timer. What you are talking about, i.e. the re-entry of
the timer handler, requires two threads, right? I don''t mean that the
programmer has asked for more than one thread, but that, somehow, under the
covers, Windows is using more than one thread. And if that''s the case don''t
you have to do something to make the setting of your beingHandled flag
atomic? Otherwise both threads could fetch and examine the beingHandled
flag before either thread sets it. How do you do the equivalent of a
critical section in .Net?

Thanks, Bob
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OM*************@TK2MSFTNGP10.phx.gbl...

Jack,

My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. Any Windows Event can "interrupt" itself by calling DoEvents since VB1.

In both VB.NET & VB6 MsgBox & MessageBox.Show calls the equivalent of
"DoEvents", I suspect to ensure the underlying form is properly displayed
(the Paint event is handled).

When you call DoEvents or its equivalent any event can occur a second


time, except I believe Paint and one other.

Firstly, are these two assumptions right No

and if so what is the approved way of stopping the event interrupting
itself?


If I call DoEvents while processing an Event, I normally set a flag in the
event that says it is already being handled.

Something like:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Static beingHandled As Boolean
If beingHandled Then Exit Sub
beingHandled = True
'' do work here
beingHandled = False
End Sub

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?


You only have to protect yourself in any event that DoEvents is explicitly
or implicitly called.

FWIW there are events that are not Windows Events, these events are not
handled via the DoEvents keyword. Windows Forms applications have a


message pump (Application.Run) that causes Windows Messages in the Windows Message
Queue to be converted into Events, each event is handled inturn, unless you call DoEvents with causes the next message in the message queue to be
converted into its respective Event. Hence if you call DoEvents explicitly
or implicitly an event can be raised a second time. This can be a source of problems if DoEvents is called with in Timer Tick, ToolBar ButtonClick,
Button Click and MenuItem Click events.

Hope this helps
Jay
"Jack Russell" <ja***@norubbish.tpg.com.au> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself.

For instance if you had a timer event containing a msgbox then you would
only get one message.

However in vb.net you get continual messages (even setting the system
modal property).

Firstly, are these two assumptions right and if so what is the approved
way of stopping the event interrupting itself?

Secondly is this true of all events in VB.net and do I have to protect
myself when I am in any event?

TIA




这篇关于vb.net中的事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆