re-use EventArgs or not

  • Thread starter Thread starter pRumpf
  • Start date Start date
P

pRumpf

Hi
I use a event in a extensive calculation method to inform a client.

therefor i implement a eventhandler and eventargs (e.g. myEvent,
myEventArgs) and a OnMyEvent(myEventArgs e) method to fire the event.

e.g.

// extensive calculation in 2 dim.
for (int x = 0; x < xMax; x++)
{

for (int y = 0; y < yMax; y++)
{

float flow = calculateflow(....)

// here i will inform the client -- create always a new
myEventArgs

myEventArgs e = new myEventArgs( x,y, xMax, yMax, flow);
OnMyEvent(e);
....


alternatively i can do:

myEventArgs e = new myEventArgs(x,y);

for (int x = 0; x < xMax; x++)
{

for (int y = 0; y < yMax; y++)
{

float flow = calculateflow(....)

// here i will inform the client - using one and the same
"e"

e.X = x;
e.Y = y;
e.Flow = flow;

OnMyEvent(e);
....


"What is the better way"
particularly with regard to performance and resources ?

Is there a "DotNet recommendation" ?


thanks
Peter
 
Generally speaking, the event args is only valid in the scope of the
event that it is being called in (at least, it should be treated that way,
logically). Also, if you don't have any properties on your event args that
require individual instances of the event args to be created (maybe there is
an object reference that is set in one of the event handlers?), then this
would be fine.

I mean, you know you are in a tight loop, and if you know that the
object will not be used beyond the initial calls to the event handlers, then
this is exactly what I would do.

If you want to get better performance, you might want to consider
passing an interface implementation to be called back. This would be faster
than calling the delegates.

Hope this helps.
 
Back
Top