473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to use WaitforSingleObject on events

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

Sep 24 '07 #1
10 8419
"blisspikle" <er***@johndenley.netschrieb
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

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

Sep 24 '07 #2
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
Sep 25 '07 #3
"blisspikle" <er***@johndenley.netschrieb
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

- 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

Sep 25 '07 #4
Yes, the event cannot occur before calling the control_update. Now
when I reset it before going into the wait, it will just wait until
the timeout. It does not seem to call the Event at all once the
manualresetevent is waiting, but the event does get called if not
using the manualresetevent. Any idea why that would be? Thanks,

Sep 26 '07 #5
"blisspikle" <er***@johndenley.netschrieb
Yes, the event cannot occur before calling the control_update. Now
when I reset it before going into the wait, it will just wait until
the timeout. It does not seem to call the Event at all once the
manualresetevent is waiting, but the event does get called if not
using the manualresetevent. Any idea why that would be? Thanks,

Are you sure that the event is raised in another thread?

Armin

Sep 26 '07 #6
On Sep 26, 12:14 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"blisspikle" <er...@johndenley.netschrieb
Yes, the event cannot occur before calling the control_update. Now
when I reset it before going into the wait, it will just wait until
the timeout. It does not seem to call the Event at all once the
manualresetevent is waiting, but the event does get called if not
using the manualresetevent. Any idea why that would be? Thanks,

Are you sure that the event is raised in another thread?

Armin
I guess that I am not sure if it is in another thread. If you
instantiate a class that you have a reference to its dll, and you use
it in your code, is it running on a seperate thread automatically? If
I do a control_update(), then I can continue on in my main code until
I get a Done() event that the dll fires. Doesn't that mean that it
is working on a seperate thread and still working while I continue on
in my main code, or maybe I do not know how this works? Thank you.

Sep 27 '07 #7
Okay, I am getting "False" from the Debug.WriteLine(InvokeRequired).
That would mean that they are on the same thread right? When
does .NET run the code for my Control_Update() then? For the
following code it seems like .NET should run the control_update, and
not return to do the msgbox("Done") until everything has finished in
that dll. Then the Processor_Done should fire and then return back to
the msgbox("Done") to return back to idle? Instead, right now it runs
Control_Update and then just posts "Done" before it fires the Done()
event. I just do not see how it is not running on another thread?

Public Class Form1

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()
Msgbox("Done")
End Sub
Public Sub Processor_Done() Handles Processor.Done
End Sub
End Class
When you run a normal Sub Main like the following one, it will not go
on and display the msgbox("Done") until done with the called CalledSub
right?

Class MyClass

Private Sub Main()
CalledSub
MsgBox("Done")
End Sub

Private Sub CalledSub()
'A loop to hold here for awhile.
End Sub

End Class

Sep 27 '07 #8
"blisspikle" <er***@johndenley.netschrieb
Here is the posting from the Call Stack Window. I have never used
this before, so it looks very messy. Thanks again for the help. I
called the solution deleteme. I am using a UI just to make it
easier to test this dll out for myself.
Deleteme.exe!Deleteme.Form1.Processor_Done() Line 26 Basic
Interop.vHMIABE.dll!vHMIABE.__CABEthernet_SinkHelp er.Done() + 0x22
bytes
[Native to Managed Transition]
[Managed to Native Transition]
[Native to Managed Transition]
[Managed to Native Transition]

The "..Transition" parts would have been most interesting. :-) Nevertheless,
no need to trace this further.. in the end we can not change it. So... the
question still is (maybe you've overlooked it at the bottom of my last post)
whether it is an option, not to wait for the event.(?)
Armin

Sep 28 '07 #9
I could do some other ways to get by the problem, I just thought that
waiting would eat up less processor time then doing loops. I could do
something with the Done Event firing and indexing a counter to go on
to the next stage? I am going to look into the Doevents, because I
have never used this and not sure what it does.

If I add a few of these processor objects to a List(of T), how can I
watch for the event to fire? Am I going to have to use addhandler for
every object that I create?

Some people have mentioned using state machines? Is there any good
links for templates on how to do state machines to control a program?

For the output below, does that mean the dll is coded in a "unmanaged"
language?
[Native to Managed Transition]
[Managed to Native Transition]
[Native to Managed Transition]
[Managed to Native Transition]
Thank you,
Erick

Sep 28 '07 #10
"blisspikle" <er***@johndenley.netschrieb
I could do some other ways to get by the problem, I just thought
that waiting would eat up less processor time then doing loops. I
could do something with the Done Event firing and indexing a counter
to go on to the next stage?
That's the best approach IMO.
I am going to look into the Doevents,
because I have never used this and not sure what it does.
Naah, please not. :-)
If I add a few of these processor objects to a List(of T), how can I
watch for the event to fire? Am I going to have to use addhandler
for every object that I create?
Yes
Some people have mentioned using state machines? Is there any good
links for templates on how to do state machines to control a
program?
I haven't heard of it, but if anybody reads this...... ?
For the output below, does that mean the dll is coded in a
"unmanaged" language?
In first place it means that MessageBox.ShowCore calls unmanaged code. It
also includes a kind of message loop like with other windows. Therefore,
also the message from the Procssor component can be processed. Though I
don't know whether it comes from a managed or unmanaged component. Mixed
mode is also possible.
[Native to Managed Transition]
[Managed to Native Transition]
[Native to Managed Transition]
[Managed to Native Transition]
You can try to enable "unmanaged debugging" in the project properties,
configuration settings. Then you should get the full call stack revealing
the unmanaged code. As I don't have you Processor component, I did it with a
Winforms Timer, setting a breakpoint in it's Tick event, so the follwing
should be similar to yours:

user32.dll!_InternalCallWinProc@20()
user32.dll!_UserCallWinProc@24()
user32.dll!_DispatchMessageWorker@8()
user32.dll!_DispatchMessageW@4()
user32.dll!_DialogBox2@16()
user32.dll!_InternalDialogBox@24()
user32.dll!_SoftModalMessageBox@4()
user32.dll!_MessageBoxWorker@4()
user32.dll!_MessageBoxTimeoutW@24()
user32.dll!_MessageBoxExW@20()
user32.dll!_MessageBoxW@16()
system.windows.forms.dll!System.Windows.Forms.Mess ageBox.ShowCore

Armin

Sep 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: herbert | last post by:
Hi everybody; I've done 2 c++ class; server and client. I would like to make the same in c#. my waiting thread was waiting for 3 type of event: - a socket have data to read - a socket want...
4
by: izik l | last post by:
I have made the following main: void main() { HANDLE myMutex = CreateMutex( NULL, false, NULL ); WaitForSingleObject(myMutex, INFINITE); printf("should never appeared"); }
4
by: Bill Sonia | last post by:
Hello, I have a Widnows Service that creates a system event that I would like to have a Sql Server stored procedure fire when the stored procedure is called. I have a Windows Service that runs...
6
by: Sean Kelly | last post by:
When both waiting on an event and a simple unnamed mutex, I'm wondering when WaitForSingleObject might return WAIT_FAILED. These are both execution paths I'd very much like to avoid exceptional...
5
by: ramialhasan | last post by:
If I have some c++ class exported from a Win32 dll, and from some other console application I created two threads and each thread creates an instance of the exported class. My question is...
1
by: Rachit | last post by:
Hi, I've a WebApp that calls Microsoft CRM WebServices to perform a particular action. In my WebApp I am spawning a new async thread for all the CRM related actions and sending the user to Thank...
1
by: stefan.sedich | last post by:
Hi I am trying to open an email in outlook with the following string command = "c:\\Program Files\\Microsoft Office\\OFFICE11\\OUTLOOK.EXE c:\\a.msg"; StartupInfo si = new StartupInfo();...
2
by: Sasie7679 | last post by:
In one of our windows XP pro with service pack2 machine, the below API's fail. 1) MsgWaitForMultipleObjectsEx 2) MsgWaitForMultipleObjects 3) WaitforSingleObject 4) WaitforSingleObjectEx In...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.