如何在事件上使用WaitforSingleObject [英] How to use WaitforSingleObject on events

查看:138
本文介绍了如何在事件上使用WaitforSingleObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多针对Waitforsingleobject API发布的示例

进程或线程,但不是事件。我无法获得

waitforsingleobject订阅活动。我使用了一个类

PLCEthernet,这是我引用的一个dll,我想

等待它提出它完成的事件,然后我继续在

我的代码。谢谢,

导入System.Runtime.InteropServices

公共类Form1


公共声明函数WaitForSingleObject Lib" kernel32" _

(ByVal hHandle As System.IntPtr,_

ByVal dwMilliseconds As Integer)作为整数

Dim WithEvents处理器作为新的PLCEthernet
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理Button1.Click


Processor.Control_Update ()

WaitForSingleObject(Processor_Done,20000)

Msgbox(完成)


End Sub


公共子Processor_Done()处理Processor.Done

结束子

结束类

I see a lot of examples posted for Waitforsingleobject API for
Processes or threads, but not events. I cannot get
waitforsingleobject to subscribe to an event. I use a class
PLCEthernet which is a dll I have a reference to, and I would like to
wait for it to raise an event that it is done, before I continue on in
my code. Thanks,
Imports System.Runtime.InteropServices
Public Class Form1

Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As System.IntPtr, _
ByVal dwMilliseconds As Integer) As Integer
Dim WithEvents Processor As New PLCEthernet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Processor.Control_Update()
WaitForSingleObject(Processor_Done, 20000)
Msgbox("Done")

End Sub

Public Sub Processor_Done() Handles Processor.Done
End Sub
End Class

推荐答案

" blisspikle" < er *** @ johndenley.netschrieb
"blisspikle" <er***@johndenley.netschrieb

我看到很多为Waitforsingleobject API发布的示例

进程或线程,但是不是事件。我无法获得

waitforsingleobject订阅活动。我使用了一个类

PLCEthernet这是一个我引用的dll,我希望

等待它引发它完成的事件,然后我在我的代码中继续

。谢谢,


Imports System.Runtime.InteropServices

公共类Form1


公共声明函数WaitForSingleObject Lib" KERNEL32" _

(ByVal hHandle As System.IntPtr,_

ByVal dwMilliseconds As Integer)As Integer


Dim WithEvents处理器为新PLCEthernet

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理Button1.Click


Processor.Control_Update()

WaitForSingleObject(Processor_Done,20000)

Msgbox(完成)


结束Sub


公共子Processor_Done()处理Processor.Done

结束子

结束类
I see a lot of examples posted for Waitforsingleobject API for
Processes or threads, but not events. I cannot get
waitforsingleobject to subscribe to an event. I use a class
PLCEthernet which is a dll I have a reference to, and I would like
to wait for it to raise an event that it is done, before I continue
on in my code. Thanks,
Imports System.Runtime.InteropServices
Public Class Form1

Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As System.IntPtr, _
ByVal dwMilliseconds As Integer) As Integer
Dim WithEvents Processor As New PLCEthernet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Processor.Control_Update()
WaitForSingleObject(Processor_Done, 20000)
Msgbox("Done")

End Sub

Public Sub Processor_Done() Handles Processor.Done
End Sub
End Class



也许我误解了,但是你可能在面向对象的环境中混淆了由

对象引发的事件,事件是多重的意思

线程同步。它们实际上没有关系。


如果你想处理托管事件,请使用Addhandler语句(或

Handles子句,因为你已经做)。如果在线程A中你想要/等于/ b / b在线程B中引发一个事件,你可以使用托管的ManualResetEvent

类:


dim mre as new manualresetevent


button1_click:

mre.waitone


Sub Processor_Done

mre.set

end sub


请注意,您也可以将超时传递给WaitOne,否则线程为<
永远锁定。


参见:
http://msdn2.microsoft.com/en-us/library/9xyf641a.aspx


Armin


Maybe I misunderstood, but probably you are mixing up the events raised by
objects in an object oriented environment with the events as a mean of multi
threaded synchronization. They do not really have a relation.

If you want to handle a managed event, use the Addhandler statement (or the
Handles clause, as you already do). If, in Thread A, you want to /wait/ for
an event raised in a Thread B, you can use the managed ManualResetEvent
class:

dim mre as new manualresetevent

button1_click:
mre.waitone

Sub Processor_Done
mre.set
end sub

Note that you can also pass a timeout to WaitOne, otherwise the thread is
locked forever.

See also:
http://msdn2.microsoft.com/en-us/library/9xyf641a.aspx

Armin


谢谢,这似乎已接近工作。我在这里显示我的最终代码

,但我想知道为什么msgbox完成显示在

msgboxIn Processor_Done之前出现。好像它应该是

其他方式?


Imports System.Threading

Imports System.Runtime.InteropServices < br $>
公共类Form1


Dim WaitforDone As New ManualResetEvent(False)

Dim WithEvents处理器作为新的PLCEthernet

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理Button1.Click

Processor.Control_Update()

WaitforDone.WaitOne(5000,True)

Msgbox(完成)

End Sub

Public Sub Processor_Done()Handles处理器。完成

Msgbox(在Processor_Done中)

WaitforDone.Set()

结束子

结束类
Thank you, that seems to be close to working. I show my final code
here, but I am wondering why the msgbox "Done" shows up before the
msgbox "In Processor_Done" shows up. It seems like it should be the
other way around?

Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1

Dim WaitforDone As New ManualResetEvent(False)
Dim WithEvents Processor As New PLCEthernet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Processor.Control_Update()
WaitforDone.WaitOne(5000, True)
Msgbox("Done")
End Sub
Public Sub Processor_Done() Handles Processor.Done
Msgbox("In Processor_Done")
WaitforDone.Set()
End Sub
End Class


" blisspikle" < er *** @ johndenley.netschrieb
"blisspikle" <er***@johndenley.netschrieb

谢谢,这似乎接近工作。我在这里显示我的最终代码

,但我想知道为什么msgbox完成显示在

msgboxIn Processor_Done之前出现。好像它应该是

其他方式?


Imports System.Threading

Imports System.Runtime.InteropServices < br $>
公共类Form1


Dim WaitforDone As New ManualResetEvent(False)

Dim WithEvents处理器作为新的PLCEthernet

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理Button1.Click


Processor.Control_Update()< br $>
WaitforDone.WaitOne(5000,True)

Msgbox(完成)


结束子


Public Sub Processor_Done()处理Processor.Done

Msgbox(In Processor_Done)

WaitforDone.Set()

End Sub

结束班
Thank you, that seems to be close to working. I show my final code
here, but I am wondering why the msgbox "Done" shows up before the
msgbox "In Processor_Done" shows up. It seems like it should be the
other way around?

Imports System.Threading
Imports System.Runtime.InteropServices
Public Class Form1

Dim WaitforDone As New ManualResetEvent(False)
Dim WithEvents Processor As New PLCEthernet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Processor.Control_Update()
WaitforDone.WaitOne(5000, True)
Msgbox("Done")
End Sub
Public Sub Processor_Done() Handles Processor.Done
Msgbox("In Processor_Done")
WaitforDone.Set()
End Sub
End Class



- 超时已到期?

- 从第二个开始事件?我还没有提到:你

必须在调用Processor.Control_Update()之前调用WaitforDone.Reset。

否则,事件在信号中之前仍然从事件中说过。

下次,即使事件

第二次没有出现,WaitOne也会立即返回。这就是为什么它被称为*手动* ResetEvent。

你也可以使用AutoResetEvent。


我认为事件不能发生在之前致电

Processor.Control_Update。

Armin


- The timeout expired?
- It happens starting with the second event? I haven''t mentioned yet: You
must call WaitforDone.Reset before calling "Processor.Control_Update()".
Otherwise, the event is in the signaled stated still from the event before.
The next time, WaitOne will immediatelly return even though the event
hasn''t occured a second time. That''s why it is called a *Manual*ResetEvent.
You can also use the AutoResetEvent.

I assume that the event can not occur prior to calling
Processor.Control_Update.
Armin


这篇关于如何在事件上使用WaitforSingleObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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