Go to content Go to navigation Go to search

Brokenwire.NET::M

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 -

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 -

Cool Tool: Royal TS
· 2008-10-10 09:43 by Thijs Kroesbergen for Brokenwire.NET

Yesterday my attention was brought to this tool "Royal TS".

Because I often work on projects involving more than just a handful of servers I use remote desktop a lot.

At first, with not too many machines around, remote desktop just works great. But as the list of machines grows I just can't remember all server names, the usernames needed and what the server is used for.

This is where Royal TS kicks in! With this (small!) application you can manage your remote desktop sessions. It will keep a list of all the servers, you can add descriptions and it will remember usernames and password.

And when running multiple sessions your task bar won't get cluttered because all sessions can run within one application. (this is optional, you can still have a window for each session)

Of course Royal TS isn't the only tool out there. I've been using Terminals as well. This open-source project may have more features, but the feel just isn't right. (The menu bars and windows do wacky things every once in a while)

So, if you ever use remote desktop, try one of these tools. I'm sure you'll never use  "Win+R -> mstsc" again ;)

Permalink -

PhotoSynth into the Matrix
· 2008-08-26 17:18 by Thijs Kroesbergen for Brokenwire.NET

I couldn't resist, I just had to make a Photosynth of the famous "Trinity Kick"

As source for the images I used a .avi file with DivX compression (that's why you see those mpeg artifacts, sorry about that). Using the .avi file instead of the DVD made the next step a lot easier: I used a nice tool named FastVideoIndexer to grab all frames of this particular scene. I used a 0.1 second interval and used frame numbers instead of running time for the file names. This resulted in 102 images for the entire "Jump & Rotate". Next I made a Photoshop batch job to "Auto Level" all images because the scene is very dark and Trinity doesn't stand out very much. The last (and easiest) step was to put all images through PhotoSynth.

Nice :)

Permalink -

Previous Next