How to properly make a thread wait for Events

junfanbl

Member
Joined
Feb 18, 2016
Messages
18
Programming Experience
1-3
I'm thinking of using the EventWaitHandle class to make a thread wait until custom events are raised. However I have never used it before and I could use some suggestions.

Can somebody please tell me what the difference is between using

VB.NET:
Dim ewh As EventWaitHandle
ewh = New EventWaitHandle(False, EventResetMode.ManualReset)
ewh.WaitOne()

To make a thread wait and

VB.NET:
Thread.Sleep()

From what I understand ewh can receive signals to open the thread by using the Set method.
VB.NET:
ewh.Set()

In my case I want to make the thread wait for a signal based on a custom event (An event in Excel). However I only have a single thread to utilize, so by making the thread wait I wont be able to send any signals to it using the Set method (which I would do if certain events occurred).

So I was thinking of using the ewh.WaitOne() method with a timer that loops. After a short time (milliseconds) of making the thread wait, open it back up to see if the event occurred. If event occurred, do x,y and z, else make the thread wait again.

What do you guys think?

By the way JohnH, my previous thread on listening to events with a script was not for a VB script. It's for a .NET script that is manipulating Excel. I just forgot to add the CreateObject("Excel.Application") method in my snippet of code which is why you probably thought it was VB. Sorry.
 
By the way JohnH, my previous thread on listening to events with a script was not for a VB script. It's for a .NET script that is manipulating Excel. I just forgot to add the CreateObject("Excel.Application") method in my snippet of code which is why you probably thought it was VB. Sorry.
Getting the terminology right will help.
VB.Net is VB (Visual Basic) and is not a scripting language, it is compiled. Microsoft moved away from "VB.Net" in 2005 and only refer to it as Visual Basic, although many including me often use "VB.Net" still.
VBA (Visual Basic for Applications) is a scripting language, code is interpreted each time it is run, it is used in Office applications.
There is also VBScript (Visual Basic Scripting Edition) that is used for desktop and html client-side scripting.

By the way, events is a means to signal code to execute when some condition occurs. You add event handler, and that is called when the event is raised.
 
Well thank you for making that clarification. I guess using "script" was not the right word. Like you said the correct terminology would help. I'm actually running the code in an interpreter which is executing the source code directly (inside its own virtual machine). So I am running VB using .NET libraries (if that is a correct way to put it). All I have to use in my development environment is an interpreter. I can't compile code into executable files or DLLs unfortunately. So I'm trying to find a way create some event driven programming. The interpreter runs like a script does; where it's run time is short and it's instructions don't stay in memory. So trying to implement event driven programming is difficult, which is why I thought of trying to manipulate the thread that the interpreter runs on, so I can sustain it in memory to check for events (if that's even possible).

As for using an event handler, I don't think the interpreter supports it. I have tried using it in the past and it won't compile. What do you think? Can I do something like that?
 
I don't think this qualifies as VB (.Net). Although you say you use .Net libraries you are not actually using the VB language and its .Net compilation. I have no knowledge of your interpreter or or its language specification.
 
Back
Top