How do events fit in?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

Will the 2nd line in the following code execute immediately? Or will it
wait until the event handler finishes?

RaiseEvent Closing()
mySocket.Close()
 
it will execute after the event handler is completed. It would only execute
syncronous (at the same time) if it was in a second thread.
 
Terry,
Have you tried the code?

All the handlers attached to the Closing event will finish running then the
socket itself will be closed. Hence Closing should pass a class derived
from System.ComponentModel.CancelEventArgs so that event handlers have a
chance to cancel closing the

Something like:

Dim e as New CancelEventArgs()
RaiseEvent Closing(me, e)
If e.Cancel Then Exit Sub
mySocket.Close()
RaiseEvent Closed(me, EventArgs.Empty)

Of course the two RaiseEvent statements should be wrapped in protected
overridable On<Event> subs per the "Design Guidelines for Class Library
Developers":

http://msdn.microsoft.com/library/d.../cpgenref/html/cpconEventNamingGuidelines.asp

http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconEventUsageGuidelines.asp

Note: within VB.NET you can simply use RaiseEvent rather then checking to
see if a delegate variable is Nothing and invoking the delegate variable...

Hope this helps
Jay
 
Events do not create new threads, they work in the same thread, hence
the next line of code will NOT get executed untill the event handler
finishes.
(but differently when in different threads).

This is a common misconception, that events create their own threads...

Clasperm
 
Terry,

Every command in a thread is executed sequentialy there will never be any
overlapping.
(When that was there was making programs would be completly impossible)

In a method it will be done command after command without any interruption
with the exception when you do doevents, wich will than be evaluated.

This does not mean that in the meanwhile another thread (with
multiprocessors parallel or otherwise serial) can do a lot of other things.

I hope this helps?

Cor
 
Back
Top