Solved

Is it possible to terminate a running function by a ButtonClick?

Posted on 2004-04-09
8
182 Views
Last Modified: 2010-04-05
Hi,

I have a function which is doing different things. These things take some time. Let's say starting some programs, copying some files, etc.
Now I'd like to have a button where I can cancel these things. So this ButtonClick should just terminate the function which is doing these things.

How can I do that?


thanks
0
Comment
Question by:Bohne
8 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10792719
Couple of options:

1.) You can use a polling method that the function must participate in:

eg

// Global var
var
 StopFunc: Boolean;

function DoSomething: Boolean;
begin

  while not(StopFunc) do
  begin
    .... do whatever
    // Let messages process
    Application.ProcessMessages;
  end;

end;

And button click can set the StopFunc:=True to cancel the routine. This does require that the function check the state variable during its execution.

2.) Take the function and move it into a thread. This would allow you to terminate the function (not always a good idea) if needed. Little more work to do this, but it does offer greater control.

Regards,
Russell


0
 
LVL 1

Expert Comment

by:psycho_cat_69
ID: 10793163
I agree with rllibby...I would use the THREAD option....Easy to control...Easy to start and stop...

Your button should call the Thread.Terminate procedure!
0
 

Author Comment

by:Bohne
ID: 10793622
Can you give me a short example on how to move a function into a thread?
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10795201
From the Delphi menu bar, go to File -> New -> Other -> Thread Object.

This will lay down a template of code for you at design time for a TThread. (See Help for TThread).

You need to put the function's code into its Execute procedure and use Terminate from the main thread (watching for it in the function's thread).

Ensure that if you do any GUI (VCL) access within the thread, you use Synchronize.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Expert Comment

by:sftweng
ID: 10795202
Alternatively, within the function, you could do a TThread.Create to execute the required logic.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10795219
There is a TThread demo program in %DELPHI%\Demos\Threads
0
 

Author Comment

by:Bohne
ID: 10797214
... sorry, dump question: How do I start the thread?!? I get only a "Create" procedure, "Execute" is protected...
When I use "Create", I get a access violation error at runtime... :-(
0
 
LVL 7

Accepted Solution

by:
sftweng earned 100 total points
ID: 10797634
As in the demo, you need to declare a descendant class of TThread to gain access to the protected "Execute" procedure, which you provide.

If you get an access violation, it may be because you're not doing the Create quite right. You need something like:

procedure StartANewThread;
var
  secondThread : TMyThread;
begin
  secondThread := TMyThread.Create(false);      // instantiate a thread and start its Exercute procedure immediately
end;

Alternatively, you could create the thread suspended and start it later. If the Delphi Help and %DELPHI%\Demos\Threads example don't give you enough to go on, let me know; I have some old thread creation code kicking around here somewhere (I just have to find it).

Alan

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now