Solved

Method execution with timeout

Posted on 2002-07-19
3
226 Views
Last Modified: 2010-04-04
Greetings, all!

I want to execute some method with timeout.
Suppose there is method foo() and I want to call it and if it had't finished in specified amount of time seconds, I want to cancel it.
How do I do that? Threads I suppose?

Respectfully yours, Eugene.
0
Comment
Question by:eugenem
3 Comments
 
LVL 6

Accepted Solution

by:
swift99 earned 50 total points
ID: 7165805
The easy way is to instantiate a thread and an event, kickoff the thread, and wait for the event to be signalled.

var
  FooEvent: TEvent;

procedure MyClass.foo
begin
  FooEvent := TEvent.Create (NIL, false, true, 'FooEvent');
  MyThread := TMyThread.Create; // Assume thread immediately executes
  WaitResult := FooEvent.WaitFor (TimeLimit); // TimeLimit is integer milliseconds
 
  case WaitResult of
  wrSignalled: HandleSuccess;
  wrTimeout: HandleTimeOut;
  wrAbandoned: HandleAbandoned;
  wrError: HandleOtherErrors;
  end;
end;

In the thread, use

procedure TMyThread.Execute;
begin
  // Various business logic
  FooEvent.SetEvent;
end;
0
 

Expert Comment

by:CleanupPing
ID: 9343027
eugenem:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9461320
eugenem,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to swift99 http:#7165805

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

25 Experts available now in Live!

Get 1:1 Help Now