all groups > dotnet clr > may 2006 >
You're in the

dotnet clr

group:

ThreadAbortException in SleepInternal


ThreadAbortException in SleepInternal Jim Sneeringer
5/7/2006 2:50:01 PM
dotnet clr: A ThreadAbortException happens about 5-6 times a day (almost 200 times in
just over two months) in the thread that sends e-mail notices. This is an
ASP.NET 2.0 web site written in C#. To improve reliability and response,
outgoing e-mails are placed in the database, and the actual sending is done
by another thread, using the code below.

Exceptions are logged, and ThreadAbortException in SleepInternal are logged
5-6 times a day. They are happening in the Thread.Sleep() in the code below.
At first I thought maybe an error was happening somewhere else in the code,
causing the application to be restarted and this tread to be aborted, but no
other exceptions are being logged. Also, there is a similar third thread that
does not experience any ThreadAbortExceptions.

There doesn’t seem to be any pattern in the times, except that it seems to
happen more often during waking hours, when I expect the web site to be in
use. Nevertheless, there are a number of exceptions in the wee hours of the
morning, when it is very unlikely that anyone at all is using the site.

As far as I know, there are no ill effect perceivable by users. If the
application is failing, it is automatically restarted, and if anyone’s
session has been interrupted, they haven’t mentioned it to me.

Here is the code:

while (true) {
Ds.EMailDataTable table = adEMail.Get();
foreach (Ds.EMailRow row in table) {
MailMessage message = new MailMessage();
message.From = new MailAddress(FixEMails(row.From));
message.Subject = row.Subject;
message.Body = row.Body;
smtp.Send(message);

// After the message is successfully sent, delete it.
adEMail.Delete(row.Key);
}
Thread.Sleep(interval);
}
RE: ThreadAbortException in SleepInternal Peter Ritchie
6/16/2006 9:51:02 AM
I can't answer why you're getting this ThreadAbortException. But, I'll
address a design issue that would make it go away:

The problem with Sleep is that it basically needlessly forces the thread to
be "busy" (not running, but can't be run until sleep is finished). With
small intervals passed to Sleep() this generally isn't an issue; but, with
larger interval values for Sleep you can cause deadlocks or poor
responsiveness in other threads.

One example is program termination. A .NET application doesn't finish
termination until all threads are completed. After a certain timeout the CLR
forces those threads to terminate (which would cause a ThreadAbortException)
by calling Abort.

I would suggest you use one or more WaitHandle's to cause the thread to
"sleep" for a specific amount of time. That way, you can correctly *ask* the
thread to wake and terminate by signalling the WaitHandle instead of blocking
one or more other threads until an Abort occurs.

--
http://www.peterRitchie.com/


[quoted text, click to view]
Re: ThreadAbortException in SleepInternal Barry Kelly
6/16/2006 7:40:08 PM
[quoted text, click to view]

Are you sure it isn't being caused by AppDomain recycling in the ASP.NET
IIS plugin?

-- Barry

--
AddThis Social Bookmark Button