Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-04-09
8
184 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 96
control image tags in a string ? 12 142
Firemonkey BASS_Init into a thread 17 30
Problem working with dynamic array - help 2 4
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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