Solved

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

Posted on 2004-04-09
8
186 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
[X]
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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month7 days, 2 hours left to enroll

622 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