Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-09
8
Medium Priority
?
189 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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