.net - Do I need to EnterWriteLock when using AddHandler? -
Using a ReadWriteLockSlim for multi-threading support, I need EnterWriteLock when using AddHandler?
Here explain my point to two examples to help you:
AddHandler ClassInstance.Event, New eventhandler (AddressOf Me.Method)
-or-
Me.ReaderWriterLockSlimInstance.EnterWriteLock () AddHandler ClassInstance.Event, New eventhandler (AddressOf Me.Method) Me.ReaderWriterLockSlimInstance.ExitWriteLock ( )
This may depend on the incident; For example, if you are using an event from a DLL, written in C #, through a "field-like event", i.e.
public events event; something avant; // synchronized
(Yes, I know you asked about VB, I explain it below ...)
And it (C. # language copy) automatically synchronized, so no multi-threading issues are not (and it helps that representatives irreversible here) However, even in the same library, events such as a non-field No can be synchronized - ie.
private event handler myField; Public event eventholder SomeEvent {// not synchronized {myField + = value; } Remove {myField - = value; }}
I know that you asked about VB ... but you are asking about the comsumer about VB, my point is That it depends on the publisher .
So if you can not control the publisher, then if you think that the manually synchronized sound advisor if you think that there is a threading risk for this scenario (mostly In cases, there is no need to synchronize, but if you know that this is a threaded area, then synchronizing is sensible).
Also note that a general pattern take An event is to take a snapshot:
protected virtual void Onsomivent () {Iventhandr Handler = some events; // value considers "field-like" version (handler! = Null) handler (this, event.rg.pt); }
My point here is that in theory it is possible for a customer to get an event even after it seems that they Unsubscribe , so if in extreme cases you can handle it manually, perhaps keeping some flag (or just exception handling) somewhere
According to the request, the VB code (for the comparison) through the previous code slice:
Safe overridable sub OnSomeEvent () as the dim handler Eventhandler = Me.SomeEvent (not the handler is nothing) then the handler.Invoke (my, EventArgs.Empty) end then End Sub
Comments
Post a Comment