473,413 Members | 1,993 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,413 software developers and data experts.

Thread Exception

Hi,
I have a problem about thread related exception handling.
I created a background thread to fill a DataTable in a Windows
Application. Inside the code, I have
foreach (…)
{
try
{
da.Fill(dt);
}
catch (ThreadAbortException)
{
// It terminates
return;
}
catch (OleDbException e)
{
// Table not found.
if (e.Message.StartsWith("The Microsoft Jet database
engine cannot find the input table or query"))
break;
else
throw;
}
finally
{
conn.Close(); // Play safe
}
}
The problem is there are three kinds of OleDbException that
could be throw:
-- Table not Found, which I've taken care of (and this
consider normal to the program because it will search through
different tables
-- Connection error: The connection doesn't exist from the
beginning.
-- Network error: The connection was there but got
disconnected.

For the Connection error and Network error, I want the main
thread to trap it, display a message and continue to run.
How do I do it?
I tried Application.ThreadException and
CurrentDomain.UnhandledException but neither works

Best Regards,
Homa
Nov 13 '05 #1
3 8943
Homa,

I think that checking against the text of the message is a bad idea.
You should check the Errors property or the ErrorCode property to see what
the specific error is.

Also, when catching the ThreadAbortException, this is a special-case
exception that is automatically re-thrown. The only way to prevent the
thread from aborting is to call the static ResetAbort method on the Thread
class so that the exception doesn't propogate up the stack.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Homa" <ho******@yahoo.com> wrote in message
news:a9**************************@posting.google.c om...
Hi,
I have a problem about thread related exception handling.
I created a background thread to fill a DataTable in a Windows
Application. Inside the code, I have
foreach (.)
{
try
{
da.Fill(dt);
}
catch (ThreadAbortException)
{
// It terminates
return;
}
catch (OleDbException e)
{
// Table not found.
if (e.Message.StartsWith("The Microsoft Jet database
engine cannot find the input table or query"))
break;
else
throw;
}
finally
{
conn.Close(); // Play safe
}
}
The problem is there are three kinds of OleDbException that
could be throw:
-- Table not Found, which I've taken care of (and this
consider normal to the program because it will search through
different tables
-- Connection error: The connection doesn't exist from the
beginning.
-- Network error: The connection was there but got
disconnected.

For the Connection error and Network error, I want the main
thread to trap it, display a message and continue to run.
How do I do it?
I tried Application.ThreadException and
CurrentDomain.UnhandledException but neither works

Best Regards,
Homa

Nov 13 '05 #2
Thank you for the advises. But how to solve the problem I mentioned? How
do I catch the exception thrown by this thread (loadTableThread) in the
main thread?

Did I made any mistake by saying
catch (OleDbException e)
{
if (e.errorCode = ...)
//handle the exception
else
throw e; // re-throw it
}
Thanks for concern,
Homa Wong

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
Hi Homa,

In your delegate that launches the background thread, you can supply a
callback method that will be called when your thread has finished running.
In that callback method, you can reconstruct the original thread delegate
and then call its EndInvoke method within a Try...Catch block. If your
background thread threw an exception that it didn't handle, calling
EndInvoke will automatically rethrow that exception.

I've got some demonstration code for this lying around somewhere if you want
it. Alternatively, if you read VB, you can download the free online chapter
at the link below that demonstrates this mechanism.

HTH,

Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128
"Homa Wong" <no*****@MyWorld.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
Thank you for the advises. But how to solve the problem I mentioned? How
do I catch the exception thrown by this thread (loadTableThread) in the
main thread?

Did I made any mistake by saying
catch (OleDbException e)
{
if (e.errorCode = ...)
//handle the exception
else
throw e; // re-throw it
}
Thanks for concern,
Homa Wong

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4

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

Similar topics

11
by: Bob Rock | last post by:
Hello, coming from win32 API I recall an ExitThread() call to gently terminate a thread from inside the same thread .... but now all I can see is an Abort call which seems to me a wrapper on the...
31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
5
by: David | last post by:
I am having a bit of a problem with catching an exception within a thread. Here is the scenario: I have a Windows Form. I create a new thread. This new thread calls a method in another DLL...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
2
by: Robinson | last post by:
Hi, I have 2 menu items on the Debug menu of my application: Throw Exception On Form and Throw Exception In Worker Thread The former just executes: throw new Exception ( "I'm testing...
4
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
0
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the...
18
by: J.K. Baltzersen | last post by:
To whomever it may concern: I am using MS Visual C++ 6.0. I have a process A which instantiates an object C. At a later point the process A creates the thread B. The thread B has access...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.