I have a class that downloads, examines and saves some large XML files. Sometimes I want the UI to tell me what's going on, but sometimes I will use the class and ignore the events. So I have placed lines of code like this in a dozen places:
RaiseEvent Report("Sending request: " & queryString)
RaiseEvent Report("Saving file: " & fileName)
RaiseEvent Report("Finished")
My question is this - will these events slow down my code if nothing is listening for them? Will they even fire?
TF: if an event occurs and there is not event handler to respond to that event, the event ins ignored.
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.
Use the EventHandler delegate for all events that don't include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event and an object for event data).
My own answer:
In VB.NET the event does NOT fire if there are no handlers set up to listen for it.
I did a little experiment where the code that raises the event passes the result of a function, and that function only executed when there was an event handler set up to handle the event.
RaiseEvent Report(GetMyString())
In other words, I am saying that the GetMystring
function above does not get called unless handlers actually exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With