Been wondering about this...
Which is better? (pseudocode - please don't grill me about semantics etc.)
public class Thing
{
public void DoLongOperation(IProgressCallback callback)
{
for (int i = 0; i < 100; i++)
{
// do stuff
callback.SetProgress(i);
}
}
}
....
MyCallBackImplementation callback = new MyCallBackImplementation;
Thing thing = new Thing();
thing.DoLongOperation(callback);
OR
public class Thing
{
public event ProgressEvent;
public void DoLongOperation()
{
for (int i = 0; i < 100; i++)
{
OnProgress(i);
}
}
}
Thing thing = new Thing();
thing.ProgressEvent += new ProgressEventHandler(Foo);
thing.DoLongOperation();
As far as I'm concerned, they achieve the same result. You will have more
control using the interface method, if you want to tell your "progress
consumer" other things than what the progress is (e.g. close a progress
window whenever you want, generally with the event method you would just
close the window when the progress is set to 100% or something - or get the
long operation to fire another event if it's Asynchronous). Although I
think the event based method is more in keeping with the whole .NET event
driven methodology and stuff.
While I'm on the subject, what do you think about the whole populating an
EventArgs style of object and passing it with your event - type-o-thing? I
suppose it depends on what you're up to, but sometimes just a couple of
plain old parameters would do. I generally use the former, for consistency,
but doesn't it take a while to create the object, populate it and destroy it
later?
Any thoughts would be greatly appreciated.
Thanks a million,
Adam