Go to content Go to navigation Go to search

Brokenwire.NET::programming

Timer.Elapsed event – The Silent Killer
· 2009-02-26 12:54 by Thijs Kroesbergen for Brokenwire.NET

Yesterday we ran into some issue where exceptions would disappear into a black hole. We had some code running within the Elapsed event of a System.Timers.Timer which seemed to crash without us being able to catch the exception in our global “unhandled exception” exception handler. It looked like we had a Ninja in our code somewhere, deadly and accurate.

So we finally figured out what was happening and I’ll explain it to you here.

To start with the code to reproduce this looks like this:
An example project can be downloaded here: SilentKiller.zip

static void Main(string[] args)
{
    Console.WriteLine("Starting...");
    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
    System.Timers.Timer aTimer;
    aTimer = new Timer(1000);
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Enabled = true;
    Console.WriteLine("Press the Enter key to exit the program.");
    Console.ReadLine();
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    Console.WriteLine("Ninja attack!");
    throw new ApplicationException("Ninja!");
    Console.WriteLine("You didn't see this one coming");
}

private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception ex = (Exception)e.ExceptionObject;
    Console.WriteLine("Caught an exception: " + ex.Message);
}

The problem here is that you’d expect the OnUnhandledException method to fire, but this doesn’t happen. It seems like the exception is silenced within the timer! It took us quite a while to find that out. But actually the MSDN documentation is accurate about this, and my colleague Paul was the first to catch this line:

In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.

So it’s not a bug, it’s a feature!

And when you peek inside the System.dll assembly from the .Net framework you’ll see the following code for the handling of the Elapsed event

private void MyTimerCallback(object state)
{
    if (state == this.cookie)
    {
        if (!this.autoReset)
        {
            this.enabled = false;
        }
        FILE_TIME lpSystemTimeAsFileTime = new FILE_TIME();
        GetSystemTimeAsFileTime(ref lpSystemTimeAsFileTime);
        ElapsedEventArgs e = new ElapsedEventArgs(lpSystemTimeAsFileTime.ftTimeLow, lpSystemTimeAsFileTime.ftTimeHigh);
        try
        {
            ElapsedEventHandler onIntervalElapsed = this.onIntervalElapsed;
            if (onIntervalElapsed != null)
            {
                if ((this.SynchronizingObject != null) && this.SynchronizingObject.InvokeRequired)
                {
                    this.SynchronizingObject.BeginInvoke(onIntervalElapsed, new object[] { this, e });
                }
                else
                {
                    onIntervalElapsed(this, e);
                }
            }
        }
        catch
        {
        }
    }
}

Now I wonder who had the guts to break the rule “Never (ever) use an empty catch”! And why did they do this?!

(This would be a nice question for a Microsoft exam ;). How many of you knew about this?)

Permalink -

Super-easy access to the Sysinternals tools
· 2009-02-18 11:54 by Thijs Kroesbergen for Brokenwire.NET

Leon Meijer blogged about the "new" way to access the sysinternals tools back in May 2008.

Today I found out another cool trick. You can not only use the  http://live.sysinternals.com/ "Open Directory" server to download the tools, you can also do the following: (Start, run, type:)

\\live.sysinternals.com\Tools

And presto! You now have access to a network share with easy and up-to-date access to the tools!

You can also map a drive letter to this share if you want to, just type:

net use * \\live.sysinternals.com\Tools

If you consider yourself an "IT-Professional" and you haven't seen or heard of any of the Sysinternals tools, shame on you! (But hey, it's never too late to learn, so go check them out!)

(This trick depends on the Windows ability to access a WebDAV share as a regular network share, so this won't work on systems where the client bit (the WebDAV mini-redirector) isn't installed (such as my Windows Server 2003)). On Windows Vista this works straight out of the box.
Another reason for some of you to leave the "antique" versions of windows behind and move on?

Permalink -

WPF Christmas Lights: see the source!
· 2008-12-13 10:37 by Thijs Kroesbergen for Brokenwire.NET

Not so very long ago I wrote about my quest to re-write the ancient Christmas Lights using brand-new technologies.

I promised to share the code with you (and write some more about it), so I published the Christmas Lights on Codeplex.

Please feel free to browse through the code, and I'll be glad to see any contributions!

Biggest points of interest at the moment:

Now repeat after me:

I will not be bored during the holidays because I have the WPF Christmas Lights to play with!

Just kidding... have fun!

Download

Permalink -

Be prepared: get the Christmas Lights
· 2008-11-05 22:06 by Thijs Kroesbergen for Brokenwire.NET

A long, long time ago I wrote about that cool christmas lights application, written by an unknown author in ancient times.

Recently I took up the plan to re-write this application using the .NET framework (check out the new logo).

I used Scott Hanselman's "BabySmash" as an inspiration to get started, and I was able to write this in a matter of hours.

 

So as we are getting closer to the holiday season, this is the time to grab your copy of the cool new Christmas Lights for you desktop from the download page. Christmas will never be the same again once you've experienced this!

The application "works" as of right now (tray-icon, blinking lights around the borders) but there is still a lot of room for improvement.

Here is a (non complete) list of features that I wish for:

Help me extend this list (and motivate me to work on this) by leaving a comment!

In the meanwhile, have fun! Oh and btw, did I mention you can download the Christmas Lights?

(More about the experiences with ClickOnce, Framework 3.5 and WPF in upcoming blog posts. And once I've tidied the sources I'll put it them here too.)

Permalink -

Tip: Batch files with over 9 parameters
· 2008-10-14 20:27 by Thijs Kroesbergen for Brokenwire.NET

This has been around for ages, but it saved a us lot of work today (once we figured this out)..

So when you are writing a batch file (.cmd file) to automate some stuff you might run in to the issue that you can only retrieve the command line parameters up to number 9, by using the %1 to %9 variables.

Example:

@echo off
SET ONE=%1
SET TWO=%2
SET THREE=%3
SET FOUR=%4
SET FIVE=%5
SET SIX=%6
SET SEVEN=%7
SET EIGHT=%8
SET NINE=%9

So what do you do when you need number ten and up? %10 and up don't exist! Solution: You use the "shift" command!

Every time you call SHIFT the parameters will move 1 position, so if you call it ten times...

SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT

then you can retrieve number 10 and up..

SET TEN=%1
SET ELEVEN=%2
SET TWELVE=%3
SET THIRTEEN=%4
SET FOURTEEN=%5
SET FIFTEEN=%6
SET SIXTEEN=%7
SET SEVENTEEN=%8
SET EIGHTEEN=%9

How about that! (Be honest, did you know this?)

Permalink -

SQL Server 2008 - RTM
· 2008-08-06 20:06 by Thijs Kroesbergen for Brokenwire.NET

Another cool product I’ve been looking forward to has been released into the wild today: SQL Server 2008 .
It is available from the MSDN subscriber site right now! Dig in, enjoy!

Now we can finally use C# as a scripting language for SSIS packages. Besides that there are a lot more exciting new features in SSIS .

Permalink -

Previous Next