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 Interrupt a thread's execution 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
27-Dec-02
Category
Win API
Language
Delphi 2.x
Views
95
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler 

Let's say that once I start a thread up, I pop up a progress window that has a 
cancel button on it to cancel execution of the thread. How do I implement this?

Answer:

To exit a thread created with TThread mid-process (as in a loop), break out of the 
loop and immediately call Terminate. This sets the Terminated flag to true. 
Following the loop you should check the Terminated status in the body of the 
Execute method; something like this:

1   procedure Execute;
2   begin
3     //...some stuff
4     while SomeCondition do
5     begin
6       // ...do some stuff
7       if CancelFlagOfSomeSort then
8       begin
9         Terminate;
10        Break;
11      end;
12    end;
13    if MyTThread.Terminated then
14      Exit;
15  end;


It's important to call Terminate because it will trigger the OnTerminate event, 
that'll allow your thread to clean up after itself. If you just break out of the 
thread and don't release resources, you'll create orphan resources in memory, and 
that is not a good thing to do.

For plain-vanilla threads, all you have to do is exit out of the thread function. That will "kill" the thread. But remember that in either case, the most important thing you have to remember is to free resources that you use during the course of the run. If you don't they'll stay there and occupy memory.

			
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