473,246 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

What happens to a thread with an unhandled exception?

Hi all

I am doing something which works, but I have a gut feel that it cannot
be relied upon. Can someone confirm this one way or the other.

I have a multi-threaded server, which responds to client logins and
sets up a thread for each active session. A thread can stay active for
a long time.

I use a subclass of threading.Thread to handle the session, to which I
have added a number of attributes and methods, plus a reference back
to the main thread. When a user logs in, I call a 'setup' method in
the thread, which among other things updates a dictionary in the main
thread to record the fact that they are logged in. When they log out,
I call a 'cleanup' method in the thread, which among other things
removes their id from the dictionary of active logins.

It sometimes happens that an unhandled exception occurs in a thread.
The main thread continues, other threads are unaffected, and users can
continue to log in. However, the user that was active at the time
cannot log back in as their cleanup method was never called.

There are a number of ways to handle this, but I found one which seems
quite effective. In the dictionary of active logins, I store a
reference to the thread which is managing the session. If the user
tries to log back in, I can detect that the thread is in a suspended
state, and I use the reference to invoke the thread's cleanup method.
This removes the entry from the dictionary of active logins and
enables the user to log in again successfully.

My worry is that the thread with the unhandled exception may
eventually get garbage-collected, in which case the cleanup method
will no longer be accessible. Could this happen, or does the thread
stay in memory until termination of the main program?

Thanks for any pointers.

Frank Millman

Jun 29 '07 #1
1 5451
On Jun 29, 2:51 pm, Frank Millman <f...@chagford.comwrote:
Hi all

I am doing something which works, but I have a gut feel that it cannot
be relied upon. Can someone confirm this one way or the other.
[...]
My worry is that the thread with the unhandled exception may
eventually get garbage-collected, in which case the cleanup method
will no longer be accessible. Could this happen, or does the thread
stay in memory until termination of the main program?
I just noticed Diez's reply to a related question earlier today, where
he suggested wrapping the thread's run() method in a try/finally
construct. It works perfectly in my situation - if there is an
unhandled exception, my cleanup method is called before the exception
is raised. Thanks, Diez (even though you did not realise you were
helping me).

Frank

Jun 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
3
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
4
by: Anders Borum | last post by:
Hello! I am working on improving my threading skills and came across a question. When working with the ReaderWriterLock class, I am getting an unhandled exception if I acquire a WriterLock with...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
20
by: joseph_gallagher | last post by:
Hi, I've recently ported a .Net 1.1 application to .Net 2.0 and the one new feature that is getting on my nerves is that when there is an unhandled exception the application completely crashes,...
10
by: Digital Puer | last post by:
Sorry for the rudimentary question. I found the following posting online and did not know the answer myself. "Reminds me of a time I had a phone interview concerning a C++ job. Didn't help...
1
by: aine_canby | last post by:
Hi, This is my Program class in my Application - static class Program { /// <summary> /// The main entry point for the application. /// </summary>
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.