Is it safe to remove and reassign events this way? If not, why?

A.Event1 := nil;
A.Event2 := nil;
  A.Event1 := MyEvent1;
  A.Event2 := MyEvent2;

Can something go wrong with it?


I've accepted Barry's answer because it answered exactly what I asked, but Vegar's answer is also correct depending on the scenario, sadly I can't accept both.


It entirely depends on what happens in the bit of code marked '...'. If it e.g. starts up a background thread and tries to invoke Event1 or Event2 after execution has continued into the finally block, you may get unexpected results.

If the code is entirely single-threaded, then yes, neither Event1 nor Event2 should be triggered while the code is between the try and finally.

However, that does assume that Event1 and Event2 properties (all Delphi events are properties of a method pointer type) do not do unusual things in their setters. A pathologically malicious event setter could squirrel away a method pointer, and still be able to invoke it, even after you later call the setter with 'nil' as the value.

But that would be highly unusual behaviour.

This sounds like an event-nightmare I have seen before :-)

Instead of removing the events, I usually set a flag that I check in the event. I often use a integer rather than boolean so that the same flag can be set multiple times in one processing.

Something like this:

procedure TMyObject.Traverse;

procedure TMyObject.OnBefore( ... );
  if FTraverseFlag > 0 then 

I guess this easily could be made thread-safe to solve Barrys concerns.

Need Your Help

Issue to align vertically content of 3 divs

html css

I'd like to align the content of 3 divs (type, desc, lang) vertically. I tried vertical-align:middle but without success. If you look at this link you will notice th...

Error while connecting to a region under a profile

python amazon-web-services boto amazon-emr

As per document region is also a parameter of boto.emr.EmrConnection class however, I get the follwoing error while making the connection:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.