Solved

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

Posted on 2004-04-09
8
183 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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