Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: Are events raised even if there are no event handlers?

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?

like image 626
Shane Miskin Avatar asked Oct 30 '08 15:10

Shane Miskin


People also ask

What happens if an event occurs and there is no event handler to respond to the event?

TF: if an event occurs and there is not event handler to respond to that event, the event ins ignored.

How are events and event handlers different?

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.

Are event listeners and event handlers the same?

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.

When would you use an event handler?

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).


1 Answers

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.

like image 142
Shane Miskin Avatar answered Sep 23 '22 19:09

Shane Miskin