View Full Version : Characteristics of Event Driven Programming - an assignment

Paul Beardsley
2014-Jun-02, 08:40 AM
In my current job, I have replaced a teacher who was teaching Event Driven Programming, using Visual Basic.

One of the assignments set before I joined required them to write an article describing the key features of Event Driven programs. I now have to mark it.

The article has to show how event handlers, trigger functions and event loops work with components on a form.

Well, an event handler is clearly the code that is run when an event occurs (e.g. a user clicks on a button or a timer reaches zero). No problem there.

An event loop is the program looping, checking on each iteration to see if any of the possible events have taken place, and to handle them if they have. So that's clear enough.

A component on a form is a button, a slider, a text box or whatever. I think they all understand that.

But what the heck is a trigger function? I imagined it was a function that is run when something is triggered (by, for example, a mouse click on a button) but how is this different from an event handler?

I looked online, and the most reliable answer (on stackoverflow.com) seems to be that it's an expression made up by assignment writers who don't really know what they're talking about.

It's hard enough to get students to complete assignments well without confusing ambiguities like this.

Or am I mistaken, and trigger functions really do have a distinct meaning that I really should know about?

2014-Jun-02, 11:23 AM
I have heard triggers on SQL Database tables called trigger functions, but mostly referred to as triggers. Some industrial software products I work with allow you to put the code you want executed on the event (i.e. mouse click, property change) selected. In this case, the code is attached to the event of the object and not in a common code base with all the other event and loop code, as in VB. The event will also pass in relevant information i.e. mouse position, object name, and page (in this case, not form) name for use in the code. I have occasionally heard this called an event function or trigger function.

a function, by definition, is a reusable piece of code that performs a specific function when called; so to me trigger 'function' is not correct since it is essentially custom code attached to an event and isn't reusable.

Jeff Root
2014-Jun-02, 12:03 PM
That sounds like simply two different, unrelated uses of the
word "function". Most words in English have more than one


The question seems oriented toward application programming
rather than operating system programming. What term(s) are
used to refer to the operating system side of events? The
programming which generates the ... triggers ... and makes
them available to applications? (Through API calls, I presume.)

At a higher level, in Windows Task Scheduler, the triggers which
trigger actions are referred to as ... "triggers".

-- Jeff, in Minneapolis

2014-Jun-02, 01:34 PM
In my experience, even though triggers and events are very similar, I describe triggers as automated supporting functions, such as when a file arrives in the in basket, do function x, or when a disk reaches 80% capacity, call function y. Then 'events' are just those things that the user causes directly/intentionally, such as a mouse click, focus change, text entry, etc.

2014-Jun-03, 06:44 PM
How did the students end up referrencing them in their assignments? Technically, it would seem that the marking should be based on what they were taught, but I suppose that's not the question. Maybe the answer depends on the progamming language. You mentioned 2 different scenarios when referrencing the event handler. 1) a user clicks on a button and 2) a timer reaches zero. Based on javascript programming, #2 might not qualify as an event handler if the code is executed via a timer within script tags. In this case, maybe #2 is a trigger function.


Interestingly, "trigger" on wikipedia mentions the trigger function and that it is related to event-driven programming, but the specific wiki page doesn't mention it.

http://en.wikipedia.org/wiki/Trigger (under technology)

2014-Jun-03, 08:09 PM
Let us know when you figure it out.

I've had a lot of experience in both Visual Basic, Visual C and even dotnet and have never heard that phrase.

The only thing I can think of is that it would be something you do programattically that causes an event to happen rather than from outside your program (user or system actions)
Something like raising an error, creating or terminating an object, or using something like sendkeys.

Paul Beardsley
2014-Jun-03, 09:13 PM
Apologies for going silent in my own thread. I haven't figured it out yet but am looking for ways to gloss over the problem!

2014-Jun-03, 09:54 PM
There is no problem.

Trigger function is a term not commonly used in .NET.
I develop software professionally for more than 30 years, use .Net since the first version and follow all the relevant blogs and I can't remember ever having heard that term.

I can assure you that "trigger function" is not a technical term in the .Net world, which VB is a part of.
Event handler is the correct technical term (in .Net).

Besides, why do they still teach VB?
VB is obsolete. Virtually nobody does new development in VB, unless it is for a VB legacy system, of which there are a few and VB programmers are still hired but it is dying.

C# is the way to go. It is the better language and as far as there is a future for .Net it will be C#.

2014-Jun-04, 12:43 AM
I am not all that great at programming, I am more of scripter than anything. In a loosy-goosy, way I would use the phrase "trigger function" as a type of response to an event handler. A handler operates when X happens.

Of course this could be my madcap way of coping with the fact I have no formal training to program.

Here is the example I would use to show the difference:

I have script to wave to a friend in a game, it watches to see if my friend Bob appears then checks to see if my hands are full. If there is something one hand but not the other, it waves the empty hand. If there is something in both hands, it checks to see if anything is a weapon. If there is no weapon, it waves the right hand by default. It can result in silly things happening. If both things are weapons (or a shield), then I get a warning that says I shouldn't wave weapons at people.

The event handled was Bob's appearance, but the response was based on a trigger function of what could be in my hands.

Again, I don't use VB much, but this is how I would think about it. I am unlikely to pass any tests in programming soon.

Jeff Root
2014-Jun-04, 01:34 PM
And I don't know what a "trigger function" is, but it
definitely sounds like a cause, not a response.

I'd guess that "trigger function" may just mean "trigger".

-- Jeff, in Minneapolis

Paul Beardsley
2014-Jun-04, 02:18 PM
From what I can gather, "trigger function" (in the context of event-driven) is poorly defined at best.

Incidentally, C# may well be better than VB, but VB is an excellent teaching tool, and is readily available. Yesterday evening I had some students with virtually no experience of programming, and none at all of ED, but within half an hour of starting up Visual Basic with them they at least had an idea of the concept.

2014-Jun-04, 09:43 PM
From what I can gather, "trigger function" (in the context of event-driven) is poorly defined at best.

Incidentally, C# may well be better than VB, but VB is an excellent teaching tool, and is readily available. Yesterday evening I had some students with virtually no experience of programming, and none at all of ED, but within half an hour of starting up Visual Basic with them they at least had an idea of the concept.

Yes, that is certainly a valid argument for VB. It depends what the goal of the course is. If you want to get hobbyist to a level where they can quickly get a piece of software together then Basic is and always has been a language of choice. It's easy to learn.

However, if the goal is to teach people who want to become professional developers I would advice against using VB. Granted, C# has a steeper learning curve but it is the better language. It was developed by some really smart people with a lot of experience in language design. And it shows. I don't know any professional developer who disagrees with that.

2014-Jun-13, 01:15 PM
VB runs its main event loop via the screens form load function as in the majority of the earlier MS Visual Studio products.


I've used MS VB 1.0 for DOS and several other MS VS products up to version 6.0 (and MS Visual c++ Express). I must say that I gained a better understanding of how the event loop operates from looking at how dbFast, a 16 bit compiled xbase for windows from the early to mid 1990's handled things. The following code shows the basic process, including screen controls, of 4 different ways to trigger the same event. It's good to see how all the code works inline without having the overall basic structure obfuscated by the screen designer.

* main program loop
* Declare public variables, menu's, screen objects etc
CREATE POPUP MENU '@Exit' from 'E@xit program
sAction = '?' *: Init sAction to something so it is not empty

DO WHILE .NOT. EMPTY(sAction) * Main Event Loop
UPDATE CONTROL ALL * Refresh screen controls
READ SAVE * wait for an event to occur i.e. after form load is run

nEvent = Event() * Event type i.e. pulldown menu (cursor), button (cursor) or keypress

nTheMenu = HMENU() * if selected, the pulldown menu number
nTheOption = VMENU() * if selected, the pulldown menu option
nTheKey = LASTKEY() * if a key was pressed, that key code
sTheButton = BUTTON() * if a button was clicked, that button name

sAction = _Translate(nEvent) * translate event type to determine the action

DO _Action * do the translated action

FUNCTION _Translate()
PRIVATE sTheAction
sTheAction = ' '
CASE nTheEvent = eClose * window close button
sTheAction = 'Done'
CASE nTheEvent = eMenu * dropdown menu
sTheAction = 'Done'
CASE nTheEvent = eButton * Button
CASE sTheButton = 'Exit'
sTheAction = 'Done'
CASE nTheEvent = eKey * Key
CASE nTheKey = 27 * Esc pressed
sTheAction = 'Done'
sTheAction = '?'

CASE sAction = 'Done'
sAction = ''

2014-Jul-07, 05:51 AM
Adding to this, in the case of windows, when any actions is taken, (mouse moved, click, button pressed, key pressed, etc...) a message is queued into the windows event queue, with the process ID the event is associated with. The main windows thread checks this queue, processing the events message in first in first out order, checking the associated processes state (busy or idle) and if idle, it sends that message to that processes event handler, in that processes thread. The handlers themselves are setup on application start (typical) and registers the event as a handle in windows, that has the location (Process ID, assembly name, function/method ID) of the event handler function, the application exit process un-registers the same events. This is why with some games and software, after three or four crashes, you may have to reboot windows, because crashing applications don't free registered events. In VB you can request windows 'Do Events' early which cause the application thread at it's current location, and start processing Events queued for that Process ID only. In C#, you have a thread for the UI/Events, and another thread (background worker) for any code that might pause the user experience for more then half a second. This allows the UI to continue on processing windows messages including form refreshing, while the longer processes are running on a separate thread. Caveats on treading: On single core CPU's, when windows or application hits 80% CPU all other additional thread requests are paused, only the application thread, and the windows thread and services threads are active, and the processing then becomes a time share model (windows gets 2 cycles, application gets 2, services get 5, or something along these lines) On Dual or more cores (with or without hyper threading) each new thread gets placed onto the least active core , each cores threads, only time share with other threads on that same core. The other cores are independent, and the 80% CPU restriction on new threads is gone. With Hyper threading, a single core can run two threads, by partitioning a single core into two slower logical cores. However most applications are too robust for logical core partitioning to help them, so windows only assigns threads to those based on the type of application (IIS web services used HT mode well, SQL server however does not.) AMD Opteron and IBM I, X and Z-Series CPU's are built to run 2 threads per core full time, without logical partitioning. A 16 core Opteron can run 32 simultaneous threads, or 64 simultaneous (but slower) threads with Hyper threading mode activated on them. Intel has yet to embrace this non logical partitioning 2 threads per core architecture.