Rob Eisenberg posted a pretty clever trick on how to avoid checking if the event delegate is null before using it. I decided to try it out and see if there was any performance penalty for calling an empty function versus checking for null. At least for me the empty function approach was slower but I really don't think this matters unless you have some really time critical piece of code. I will be using the empty function trick henceforth.


The code I used to perform the test was:

class EventTimeTest
{
public event EventHandler StandardEvent;
public event EventHandler TrickyEvent = delegate { };

public void Test()
{
Stopwatch timer;

EventArgs e = new EventArgs();
for (int testCase = 0; testCase < 9; testCase++)
{
int limit = (int)Math.Pow(10, testCase);

timer = new Stopwatch();
timer.Start();
for (int iteration = 0; iteration < limit; iteration++)
{
OnStandardEvent(e);
}
timer.Stop();
double standardMs = timer.ElapsedMilliseconds;
double standardTicks = timer.ElapsedTicks;

timer = new Stopwatch();
timer.Start();
for (int iteration = 0; iteration < limit; iteration++)
{
OnTrickyEvent(e);
}
timer.Stop();

double msDifference = standardMs != 0 ? timer.ElapsedMilliseconds/standardMs : 0;
double tickDifference = standardTicks != 0 ? timer.ElapsedTicks/standardTicks : 0;

Console.WriteLine("{0}: MS Standard: {1} Tricky: {2} Difference {3}",
limit, standardMs, timer.ElapsedMilliseconds, msDifference);
Console.WriteLine("{0}: Ticks Standard: {1} Tricky: {2} Difference {3}",
limit, standardTicks, timer.ElapsedTicks, tickDifference);

}
}

private void OnStandardEvent(EventArgs e)
{
if (StandardEvent != null)
StandardEvent(this, e);
}

private void OnTrickyEvent(EventArgs e)
{
TrickyEvent(this, e);
}
}

Running these tests two times, the first without subscribing to the events and the with subscribing to the events.
My results:


Without subscribing:


1: MS Standard: 0 Tricky: 0 Difference 0
1: Ticks Standard: 334528 Tricky: 414792 Difference 1,23993208341305
10: MS Standard: 0 Tricky: 0 Difference 0
10: Ticks Standard: 4288 Tricky: 6840 Difference 1,59514925373134
100: MS Standard: 0 Tricky: 0 Difference 0
100: Ticks Standard: 3984 Tricky: 5864 Difference 1,4718875502008
1000: MS Standard: 0 Tricky: 0 Difference 0
1000: Ticks Standard: 23160 Tricky: 41544 Difference 1,79378238341969
10000: MS Standard: 0 Tricky: 0 Difference 0
10000: Ticks Standard: 208584 Tricky: 397904 Difference 1,90764392283205
100000: MS Standard: 0 Tricky: 1 Difference 0
100000: Ticks Standard: 2063880 Tricky: 4035024 Difference 1,95506715506716
1000000: MS Standard: 8 Tricky: 15 Difference 1,875
1000000: Ticks Standard: 21348944 Tricky: 42312056 Difference 1,98192734966189
10000000: MS Standard: 81 Tricky: 151 Difference 1,8641975308642
10000000: Ticks Standard: 217051903 Tricky: 403840296 Difference 1,86057016970729
100000000: MS Standard: 813 Tricky: 1530 Difference 1,88191881918819
100000000: Ticks Standard: 2166929311 Tricky: 4074169320 Difference 1,8801579263884

With subscribing:


1: MS Standard: 0 Tricky: 0 Difference 0
1: Ticks Standard: 196896 Tricky: 231644 Difference 1,17647895335609
10: MS Standard: 0 Tricky: 0 Difference 0
10: Ticks Standard: 2472 Tricky: 3192 Difference 1,29126213592233
100: MS Standard: 0 Tricky: 0 Difference 0
100: Ticks Standard: 6408 Tricky: 12120 Difference 1,89138576779026
1000: MS Standard: 0 Tricky: 0 Difference 0
1000: Ticks Standard: 46048 Tricky: 100264 Difference 2,17738012508687
10000: MS Standard: 0 Tricky: 0 Difference 0
10000: Ticks Standard: 442368 Tricky: 2101134 Difference 4,74974229600694
100000: MS Standard: 1 Tricky: 3 Difference 3
100000: Ticks Standard: 4443286 Tricky: 9994829 Difference 2,24942283706248
1000000: MS Standard: 17 Tricky: 38 Difference 2,23529411764706
1000000: Ticks Standard: 45348919 Tricky: 102265594 Difference 2,25508339019062
10000000: MS Standard: 170 Tricky: 379 Difference 2,22941176470588
10000000: Ticks Standard: 455012748 Tricky: 1010235251 Difference 2,22023504932657
100000000: MS Standard: 1711 Tricky: 3849 Difference 2,24956165984804
100000000: Ticks Standard: 4555073376 Tricky: 10247927123 Difference 2,24978310492094
I can't remember where I first learned about this little trick but know I use it all the time when when updating GUI controls in C#. You are not allowed to access an GUI object from another thread using the dotnet framework 2.0. To be able to update the GUI from another thread you have to use the Invoke command of the control using a delegate. In the beginning I wrote code like this:

private delegate void SetText(string text);
private void UpdateTitle(string title)
{
if (lblTitle.InvokeRequired)
{
SetText setText = UpdateTitle;
lblTitle.Invoke(setText, new object[] {title});
}
else
{
lblTitle.Text = title;
}
}

The next step was to try and make it bit more general by doing:

private void UpdateText(Control control, string text)
{
if (control.InvokeRequired)
{
SetText setText = UpdateTitle;
control.Invoke(setText, new object[] {text});
}
else
{
control.Text = text;
}
}

I even went so far as to write a general wrapper class using generics to facilitate this Invoke pattern. It really bugged me that I had to write all this code just to update the damn control. Then one day I saw this neat little trick using anonymous delegates which I know use and love.

protected delegate void Func();

protected void InvokeIt(Func f)
{
if (InvokeRequired)
Invoke(f);
else
f();
}

private void UpdateTitle(string title)
{
InvokeIt(delegate { lblTitle.Text = title; });
}

I usually place the InvokeIt in a sub classed Form or UserControl from which I inherit. By using the anonymous delegate I can call pretty much whatever code I want and I don't have to write new delegates for every property I want to update. I do hope that there will come some compiler support so the Invoke will be done automatically if needed. But until then, or until I find some even more clever way of doing this, I will use Invoke with an anonymous delegate.
This is going to be my futile attempt at documenting my struggle to better myself as a programmer. At first I reckon that I will mostly link to other blogs and articles which have written something interesting, cool, worthwhile, or funny. To keep this from getting yet another linkblog, I will try to write at least a couple of sentences why I found the article interesting.
Since I'm from Sweden and English is not my native language I apologize beforehand on any and all spelling mistakes, grammar quirks, poor choice of words, etc.

Welcome!