Adding/removing the delegate actually involves running code inside the
type providing the event, so that will certainly take more time than
setting/clearing a boolean.
Keep in mind that if the method gets called multiple times, unnecessarily,
then not only do you have overhead in the method actually being executed
(code being run) but the reference to the "sender" must be passed as a
parameter and a reference to the DataColumnChangeEventArgs object will need
to be passed too as well as a memory allocation required to make room on the
heap for the DataColumnChangeEventArgs object data itself. So depending on
the situation adding/removing a delegate could very well be faster, and more
resource friendly, then the method of a supposed simple boolean check. Now I
haven't actually run a test to see how these two methods compare right down
to the tick count but you have to consider that there is more going on here
then just "setting/clearing a boolean value". But the truth is that in the
application that I'm currently working on I'm using both methods. If I know
that the amount of times that the method will be getting called is minimal,
then I will use a boolean. But if the possibility exists that the method may
get called many times then I will unhook from the event and then hook in
again after the work is done. This is something that has to be seen by
someone who is looking at the entire scope of what, at least, a certain
section of the application is doing. It's not something that you can say
just by answering a simple newsgroup question. So the decision has to be
made as to how many times this method would get called and will unhooking
and rehooking be a better or worse solution in this situation. For every
item that needs to be updated in the ColumnChanged event you need to push
parameters onto the stack and also allocate memory that is not even going to
be accessed since the method will get called again only to outright fail
when the boolean is checked. Also a well known way of adding a new row is to
use the NewRow method of the DataTable and then proceed to fill in the
columns one at a time with information. This will call the ColumnChanged
event for every column of data that is set during this process. Then the
event handler will be called 3 times when in this situation, since you are
just setting up new row data, this event does not need to be called at all.
At 3 times the number of unneccessary memory allocations per column of data
changed that can add up to a whole lot more time taken then just unhooking
from the event and then hooking back up when done.
Dim newRow As DataRow = Me.myDataTable.NewRow
newRow.Item("StringColumn") = "String" ' *
newRow.Item("IntegerColumn") = 0 ' *
Me.myDataTable.Rows.Add(newRow)
* ColumnChanged event called 3 x times (once for the change here and then
once for each "Update" change in the ColumnChanged event).
Now wrap it with the "BeginDataTableUpdate()" and "EndDataTableUpdate()"
calls and you have the overhead of removing and then adding the delegate but
you do not call the event handler 6 times for just two columns. And this
just gets worse the more columns you have. So in a situation where you are
reentering and executing code in a method just to check a boolean can in
fact allocate more memory and take more time depending on how many times the
method is getting called. I did it this way not only for the reasons I
mentioned previously but for the fact that this is a way to essentially
ensure that the event handler is not getting called at all. But the original
poster will need to look at this situation and decide which solution best
fits the given problem. But, as I mentioned earlier, using both methods
might be the way to go.