Question

I've inherited a project that, when started in debug, throws somewhere in the avenue of 8-10k NotImplementedExceptions. Because of this, the application takes over a minute to start when debugging... Every. Single. Time.

I talked to the original devs of the project and their solution to the issue is "Just press Ctrl+F5 to start without attaching a debugger". Apart from that being one of the worst solutions to a problem in the history of development, it does work and the application starts immediately. I'm trying to learn this new code-base so just skipping the debugger isn't an option for me.

Obviously I'm aware that no application should start with 10 thousand exceptions, but before I even get to fixing that I have to be able to debug the program.

I would like to suppress or skip first chance exceptions in the same way that starting without the debugger attached does. I've looked at this thread and applied the [DebuggerNonUserCode] attribute to the related methods but it only prevented the exceptions from being written to the output window. The program still takes over a minute to start up. Is this possible?

Edit:

I forgot to mention, I don't have anything checked in the Debug->Exceptions window. Also, all of the exceptions are wrapped in Try.. Catch statements

Was it helpful?

Solution

  1. Check your Visual Studio settings under Debug -> Exceptions -> Common Language Runtime Exceptions -> System -> System.NotImplementedException and make sure that Thrown is NOT checked. (it should be not checked by default, but this would cause it to stop even if exception were to be handled).
  2. Crappy workaround: In the part of the code which is past all exceptions, where you want to start debugging, put the following lines

    System.Diagnostics.Debugger.Launch();
    System.Diagnostics.Debugger.Break();
    

You can then start Ctrl+F5, and then be prompted to attach debugger when it hits that point in the code.

OTHER TIPS

EDIT: Major edit to remove my early-morning brain damage.

To provide some additional info for my point, to try to show steps that can be used to reduce the startup time for this number of exceptions. This code allows me to profile N calls with or without the exception being thrown:

void Run(int n, bool doThrow, bool doExcept)
{
    for (int i = 0; i < n; i++)
    {
        if (doExcept)
        {
            try
            {
                if (doThrow)
                    throw new NotImplementedException();
            }
            catch (NotImplementedException)
            {
            }
        }
    }
}

The testing scenario gives the following numbers when no checkmarks are set in the Exceptions dialog, in Debug mode with the VS hosting process.

1; 4.000 msec
1: 0.000 msec (no throw)
1: 0.000 msec (no throw, no except)
10; 32.000 msec
10: 0.000 msec (no throw)
10: 0.000 msec (no throw, no except)
100; 780.000 msec
100: 0.000 msec (no throw)
100: 0.000 msec (no throw, no except)
1000; 7251.000 msec
1000: 0.000 msec (no throw)
1000: 0.000 msec (no throw, no except)
10000; 35694.000 msec
10000: 0.000 msec (no throw)
10000: 0.000 msec (no throw, no except)

So clearly the number of exceptions is what matters here, as you expected. I'll edit with some more numbers from different scenarios in a minute.

In debug with the VS hosting process, the exceptions take about 3.5-7 msec. Disabling the VS hosting process (which still allows debugging) in debug mode drops the time down to 1.9 msec per exception:

1; 3.000 msec
10; 21.000 msec
100; 198.000 msec
1000; 1917.000 msec
10000; 19065.000 msec

And release mode without the VS hosting process takes about 1.8 msec (edit after plugging in my laptop):

1; 3.000 msec
10; 17.000 msec
100; 184.000 msec
1000; 1823.000 msec
10000; 18248.000 msec

And running in release mode outside of the debugger is about 0.016 msec per exception:

1; 1.000 msec
10; 1.000 msec
100; 3.000 msec
1000; 19.000 msec
10000; 138.000 msec

So in my test setup, it still doesn't load "instantly" from the debugger with 10k exceptions in release. Though, this could be related to the problems I experienced before with my 2010 installation (I've since reinstalled). It does load extremely quickly outside of the debugger.

Here is my full testing rig:

static void Main(string[] args)
{
    Stopwatch sw = new Stopwatch();
    var p = new Program();

    int[] tests = new int[] { 1, 10, 100, 1000, 10000 };
    foreach (int n in tests) {
        sw.Start();
        p.Run( n, true, true);
        sw.Stop();
        Console.WriteLine(string.Format("{0}; {1:0.000} msec", n, sw.ElapsedMilliseconds));

        sw.Start();
        p.Run(n, false, true);
        sw.Stop();
        Console.WriteLine(string.Format("{0}: {1:0.000} msec (no throw)", n, sw.ElapsedMilliseconds));

        sw.Start();
        p.Run(n, false, false);
        sw.Stop();
        Console.WriteLine(string.Format("{0}: {1:0.000} msec (no throw, no except)", n, sw.ElapsedMilliseconds));
    }

    Console.WriteLine("Any key to exit...");
    Console.ReadKey();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top