First exception is very slow...

R

Robert Gravereaux

I've noticed that the first exception thrown/caught by an app running in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4 machine. I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and hook it to the
following code:
private void btnException_Click(object sender, System.EventArgs e)
{
try
{
throw new ApplicationException("Exception!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Run the app in debug (F5), click on the button, and wait about 5-10 sec. for
the message box to appear. Clicking a second time causes the message box to
appear immediately.

Can anyone tell me what's causing this, and is there a solution/workaround?

Thanks.

--
Rob Gravereaux
Principal Developer
Glen Road Systems, Inc.
(e-mail address removed)
http://www.grsinc.com
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

Catching exceptions is always a more expensive operation. The runtime
might hold of until a first occurance in order to save some time. The only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.
 
R

Robert Gravereaux

Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not occur if
I run the compiled executable, so I don't think I'd want to add additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

Nicholas Paldino said:
Robert,

Catching exceptions is always a more expensive operation. The runtime
might hold of until a first occurance in order to save some time. The only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
I've noticed that the first exception thrown/caught by an app running in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4 machine. I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and hook it to the
following code:
private void btnException_Click(object sender, System.EventArgs e)
{
try
{
throw new ApplicationException("Exception!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Run the app in debug (F5), click on the button, and wait about 5-10 sec. for
the message box to appear. Clicking a second time causes the message box to
appear immediately.

Can anyone tell me what's causing this, and is there a solution/workaround?

Thanks.

--
Rob Gravereaux
Principal Developer
Glen Road Systems, Inc.
(e-mail address removed)
http://www.grsinc.com
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

Of course! The fact that it is being debugged in the first place
changes a lot. You have one process basically hooking into another process
as it is executing code. I can't imagine that there ^wouldn't^ be any
overhead involved in this process, as well as initialization and tear-down
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not occur if
I run the compiled executable, so I don't think I'd want to add additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

message news:%[email protected]...
Robert,

Catching exceptions is always a more expensive operation. The runtime
might hold of until a first occurance in order to save some time. The only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
I've noticed that the first exception thrown/caught by an app running in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4 machine. I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and hook it
to
the
following code:
private void btnException_Click(object sender, System.EventArgs e)
{
try
{
throw new ApplicationException("Exception!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Run the app in debug (F5), click on the button, and wait about 5-10
sec.
for
the message box to appear. Clicking a second time causes the message
box
to
appear immediately.

Can anyone tell me what's causing this, and is there a solution/workaround?

Thanks.

--
Rob Gravereaux
Principal Developer
Glen Road Systems, Inc.
(e-mail address removed)
http://www.grsinc.com
 
R

Robert Gravereaux

OK, I'll accept that, in general. however 5 - 10 seconds seems excessive,
and for the 5 - 10 seconds I'm waiting nothing appears to be happening, i.e.
no disk activity, no libraries appear to be loading, etc...

For kicks I ran the same code in debug in VS.Net 2002 on the same machine
and I get what I'd expect - the catch block is executed immediately the
first time. So I still think my issue is valid. I guess this is either a
configuration issue in VS.Net 2003 or, dare I say it, a bug.

Can you replicate this behaviour in your environment? I can't believe this
is 'normal' for VS.Net 2003, yet it happens on every install I have.

Nicholas Paldino said:
Robert,

Of course! The fact that it is being debugged in the first place
changes a lot. You have one process basically hooking into another process
as it is executing code. I can't imagine that there ^wouldn't^ be any
overhead involved in this process, as well as initialization and tear-down
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not occur if
I run the compiled executable, so I don't think I'd want to add additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

message news:%[email protected]...
Robert,

Catching exceptions is always a more expensive operation. The runtime
might hold of until a first occurance in order to save some time. The only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I've noticed that the first exception thrown/caught by an app
running
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

If you email me a zipped project, I can take a look at it on my machine.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
OK, I'll accept that, in general. however 5 - 10 seconds seems excessive,
and for the 5 - 10 seconds I'm waiting nothing appears to be happening, i.e.
no disk activity, no libraries appear to be loading, etc...

For kicks I ran the same code in debug in VS.Net 2002 on the same machine
and I get what I'd expect - the catch block is executed immediately the
first time. So I still think my issue is valid. I guess this is either a
configuration issue in VS.Net 2003 or, dare I say it, a bug.

Can you replicate this behaviour in your environment? I can't believe this
is 'normal' for VS.Net 2003, yet it happens on every install I have.

message news:[email protected]...
Robert,

Of course! The fact that it is being debugged in the first place
changes a lot. You have one process basically hooking into another process
as it is executing code. I can't imagine that there ^wouldn't^ be any
overhead involved in this process, as well as initialization and tear-down
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not
occur
if
I run the compiled executable, so I don't think I'd want to add additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message Robert,

Catching exceptions is always a more expensive operation. The runtime
might hold of until a first occurance in order to save some time. The
only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I've noticed that the first exception thrown/caught by an app
running
in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4 machine.
I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and hook
it
to
the
following code:
private void btnException_Click(object sender, System.EventArgs e)
{
try
{
throw new ApplicationException("Exception!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Run the app in debug (F5), click on the button, and wait about
5-10
sec.
for
the message box to appear. Clicking a second time causes the
message
box
to
appear immediately.

Can anyone tell me what's causing this, and is there a
solution/workaround?

Thanks.

--
Rob Gravereaux
Principal Developer
Glen Road Systems, Inc.
(e-mail address removed)
http://www.grsinc.com
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

I am getting approximately the same results (3 seconds on the first one,
none on the subsequent calls). I can only guess, but I imagine it has
something to do with the way that the CLR exposes debugging functionality,
and possibly with the way that VS.NET hooks up with the CLR for debugging,
and the possibility of those things changing between .NET 1.0 and .NET 1.1.

However, when running a release version, I do not get this.

Given that the versions we run are debug versions, I can't say I am
extremely concerned. I can ask around why this might happen though, and
post if I find anything.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
Here's a sample app. It's the same code from my first posting, with the
addition of some timing code.

Please run it in debug from the VS.Net 2003 IDE, click the button and wait.
On my environments the first exception takes anywhere from 3 to 10 seconds
to be caught. After that it's immediate.

In VS.Net 2002 the first exception is caught immediately.

Let me know what your times are.

Thanks.

message news:[email protected]...
Robert,

If you email me a zipped project, I can take a look at it on my machine.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
OK, I'll accept that, in general. however 5 - 10 seconds seems excessive,
and for the 5 - 10 seconds I'm waiting nothing appears to be
happening,
i.e.
no disk activity, no libraries appear to be loading, etc...

For kicks I ran the same code in debug in VS.Net 2002 on the same machine
and I get what I'd expect - the catch block is executed immediately the
first time. So I still think my issue is valid. I guess this is either a
configuration issue in VS.Net 2003 or, dare I say it, a bug.

Can you replicate this behaviour in your environment? I can't believe this
is 'normal' for VS.Net 2003, yet it happens on every install I have.

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message Robert,

Of course! The fact that it is being debugged in the first place
changes a lot. You have one process basically hooking into another
process
as it is executing code. I can't imagine that there ^wouldn't^ be any
overhead involved in this process, as well as initialization and tear-down
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not occur
if
I run the compiled executable, so I don't think I'd want to add
additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

in
message Robert,

Catching exceptions is always a more expensive operation. The
runtime
might hold of until a first occurance in order to save some
time.
The
only
way I can think of to get around this would be to create a new thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
I've noticed that the first exception thrown/caught by an app
running
in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4
machine.
I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and
hook
it
to
the
following code:
private void btnException_Click(object sender,
System.EventArgs
 
R

Robert Gravereaux

I'd appreciate anything you can find out.

Thanks for the effort.


Nicholas Paldino said:
Robert,

I am getting approximately the same results (3 seconds on the first one,
none on the subsequent calls). I can only guess, but I imagine it has
something to do with the way that the CLR exposes debugging functionality,
and possibly with the way that VS.NET hooks up with the CLR for debugging,
and the possibility of those things changing between .NET 1.0 and .NET 1.1.

However, when running a release version, I do not get this.

Given that the versions we run are debug versions, I can't say I am
extremely concerned. I can ask around why this might happen though, and
post if I find anything.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Gravereaux said:
Here's a sample app. It's the same code from my first posting, with the
addition of some timing code.

Please run it in debug from the VS.Net 2003 IDE, click the button and wait.
On my environments the first exception takes anywhere from 3 to 10 seconds
to be caught. After that it's immediate.

In VS.Net 2002 the first exception is caught immediately.

Let me know what your times are.

Thanks.

message news:[email protected]...
Robert,

If you email me a zipped project, I can take a look at it on my machine.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

OK, I'll accept that, in general. however 5 - 10 seconds seems excessive,
and for the 5 - 10 seconds I'm waiting nothing appears to be happening,
i.e.
no disk activity, no libraries appear to be loading, etc...

For kicks I ran the same code in debug in VS.Net 2002 on the same machine
and I get what I'd expect - the catch block is executed immediately the
first time. So I still think my issue is valid. I guess this is
either
a
configuration issue in VS.Net 2003 or, dare I say it, a bug.

Can you replicate this behaviour in your environment? I can't
believe
this
is 'normal' for VS.Net 2003, yet it happens on every install I have.

in
message Robert,

Of course! The fact that it is being debugged in the first place
changes a lot. You have one process basically hooking into another
process
as it is executing code. I can't imagine that there ^wouldn't^ be any
overhead involved in this process, as well as initialization and
tear-down
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks for the reply.

It only happens when running apps in debug in the IDE. It does not
occur
if
I run the compiled executable, so I don't think I'd want to add
additional
code if possible.

Is this behaviour affected by any of the settings of the debugger?

"Nicholas Paldino [.NET/C# MVP]"
wrote
in
message Robert,

Catching exceptions is always a more expensive operation. The
runtime
might hold of until a first occurance in order to save some time.
The
only
way I can think of to get around this would be to create a new
thread,
throwing and catching an exception on that thread, and then exiting.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
I've noticed that the first exception thrown/caught by an app
running
in
debug is very slow - it takes perhaps 7 or 8 seconds on my P4
machine.
I've
noticed this on several different machines running the development
environment.

To replicate, create a new c# windows app, add a button, and hook
it
to
the
following code:
private void btnException_Click(object sender,
System.EventArgs
e)
{
try
{
throw new ApplicationException("Exception!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Run the app in debug (F5), click on the button, and wait about
5-10
sec.
for
the message box to appear. Clicking a second time causes the
message
box
to
appear immediately.

Can anyone tell me what's causing this, and is there a
solution/workaround?

Thanks.

--
Rob Gravereaux
Principal Developer
Glen Road Systems, Inc.
(e-mail address removed)
http://www.grsinc.com
 
J

Jon Skeet [C# MVP]

Robert Gravereaux said:
Here's a sample app. It's the same code from my first posting, with the
addition of some timing code.

Please run it in debug from the VS.Net 2003 IDE, click the button and wait.
On my environments the first exception takes anywhere from 3 to 10 seconds
to be caught. After that it's immediate.

In VS.Net 2002 the first exception is caught immediately.

Let me know what your times are.

It's just over a second for me. I noted that during that second, the
CPU is busy - so it's not like the machine is just waiting or anything.

Here's a rather simpler program to demonstrate it - less GUI fluff to
get in the way:

using System;

class Test
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
try
{
throw new ApplicationException("Hello");
}
catch (Exception)
{
DateTime end = DateTime.Now;
Console.WriteLine(end-start);
}
Console.ReadLine();
}
}

Now, the interesting thing is that if you change ApplicationException
to Exception, the delay goes away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Living With a Computer 3

Top