Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To KILL Procedure/Action From Another One??? 1

Status
Not open for further replies.

BoMi

Programmer
Jul 20, 2006
39
0
0
RS
Hi to all :)!
Well, I have been looking for some time for the answer for this question. I haven't found answer in Delphi or Windows SDK Help files or even on the internet after some searching...

Is there an easy (or hard) and efficient (or not) way for killing/terminating a procedure (or action from the action menu), which is running at that moment, from another procedure/action?
I know there are procedures for killing processes/threads, but my procedures/actions would be from the same exe/process, and, basically, I would like to have e.g. two buttons, presing one would start some action, and pressing the second would stop the current action right then, and would set everything to the state before action actually started...
Any help will be highly appreciated...
 
Actually, having a "Cancel" flag in any process you want to be cancellable and checking it every time it is possible.

Something like that:

var
Cancel : boolean;

procedure MyCancellableProc;
begin
some_work;
if not Cancel
then another_work;
if not Cancel
then still_another_work;
if Cancel
then reverse_things_back;
end;

procedure TSomeForm.CancelButtonClick(...)
begin
Cancel := True;
end;

Now, the point is how to have your CancelButton responsive while some CPU intensive work is being done.

In a single threaded application, liberally spreading Application.ProcessMessages through the process code can achieve it, but I personally don't like it to much.

The best way, IMO, is having your work process in a secondary thread. This way you can have your process fully working and a responsive interface.

Note: if your work is in a secondary thread, you don't need a Cancel flag, you can use the Terminated thread property and the Terminate method:

procedure MyThread.Execute;
begin
some_work;
if not Terminated
then another_work;
if not Terminated
then still_another_work;
if Terminated
then reverse_things_back;
end;

procedure TSomeForm.CancelButtonClick(...)
begin
MyThread.Terminate;
thread_synch_and_cleanup_as_needed;
end;

HTH.
buho (A).

 
Thanks buho for your answer. I appreciate your help.
I thought, some time ago, to do something like your first tip, but I don't like it much too. And, I also hopped I could escape threads, in this case at least. But I guess it's the best way, it looks nice to me. I'll try to implement what you suggested.
Thank you again for your help.
 
There's really not any escaping the use of threads. I find in any good application that takes longer than a moment to complete, it's a requirement to use threads. Speaking of which, that makes me wonder why that hasn't been set into the compiler by default, so that chore isn't necessary for everything you do.
 
I agree with you Glenn9999, but I didn't use much threads in past, cause I've never really needed them (simply applications, also short ones). So I didn't know to much about that topic. I've learnt a bit in last days, and I can at least say they are very powerful tool...

To buho:
I did as you suggested and it works perfectly.
Thank you very very much for all your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top