Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use Exception handling in threads Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
15-Sep-02
Category
Win API
Language
Delphi 3.x
Views
66
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Gerbert 

Exception handling in threads

Answer:

One of my applications that ran fine on my system, particularly when run in the 
Delphi IDE, started to stop executing after displaying 'Application error has 
occurred'.
What was the reason?

I had moved I/O-depending parts of that application into separate threads in order 
to increase performance and improve responsiveness of the application to user 
input. It was a typical scenario, ideal for multi-threading.

It turned out that those threads threw an unexpected exception. I did have a global 
exception handler as a method of my main form/ property of TApplication, but that 
did not catch the exceptions raised by threads other than the main thread.

Therefore it is mandatory to do at least some basic exception handling on your 
threads. The simplest solution is to put a try-except-end block in the Execute 
method and silently eat all exceptions.

If you want to display the exception remember that the VCL itself is not 
thread-safe. You can make it threadsafe for the execution time of a method by 
calling this method with the Synchronize() function. The downside is that you 
cannot pass arguments to a synchronized function. You have to pass arguments via 
member variables of your thread class.

1   
2   // This is the cheapest way to handle exceptions
3   // in threads. if you want to display the exception
4   // then you need to do this with a separate method,
5   // which has to be called synchronized..
6   
7   procedure TSortThread.Execute;
8   begin
9     try
10      // do the sorting
11    except
12      // silently eat all exceptions
13    end;
14  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC