473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Run time limit for function

Hi,

I would like to run few lines of code and limit the time. If the
procedure would not be finished say in 1000ms I would like to stop
processing it.

Unfortunately I do not know where to begin to find resources how to do
it.

Thanks

--
Martin

Mar 29 '07 #1
3 4104
Put it in a thread
http://builder.com.com/5100-6373-1044970-2.html

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog

"markon" <ma****@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
Hi,

I would like to run few lines of code and limit the time. If the
procedure would not be finished say in 1000ms I would like to stop
processing it.

Unfortunately I do not know where to begin to find resources how to do
it.

Thanks

--
Martin

Mar 29 '07 #2
On Thu, 29 Mar 2007 07:10:18 -0700, markon <ma****@gmail.comwrote:
I would like to run few lines of code and limit the time. If the
procedure would not be finished say in 1000ms I would like to stop
processing it.

Unfortunately I do not know where to begin to find resources how to do
it.
IMHO, the simplest solution is to use the DateTime class to keep track of
the time spent processing, checking it at a convenient place (or places)
in your processing (not too often, otherwise the checking the time will
start significantly slowing the processing itself down). When the time
spent exceeds the limit you have in mind, you exit your processing.

If you simply move the processing into another thread, you could stop the
processing by just terminating that thread but that's not a very clean way
to handle stopping the processing, and depending on the nature of the
processing may prevent you from being able to access a coherent set of
data representing the results of the processing (since you have no way to
control *where* in the processing the thread was terminated).

As an alternative to simply checking the time, you could use (for example)
interlocked access to a flag that is checked within the processing code
(in the same place or places you might check the time), run the processing
code in one thread and have a different thread monitoring the time. When
the time expires, the different thread would set the flag, the processing
code would notice that the flag has been set, and again exit cleanly.

(Actually, in practice you could probably get away without any
synchronized access to the flag, as long as the variable is marked
volatile, since the only change to the variable will ever to be to set the
flag).

The advantage of using a flag is that you can provide a number of
mechanisms to stop the processing, all of which use the event to signal
the thread to stop. You might have a thread monitoring the time (for
example, just set a timer on a form that expires when you want processing
to stop), and then also have a button on a form that the user can press to
stop processing.

Hope that helps.

Pete
Mar 29 '07 #3
Hi,

There is no real way of doing it, Windows is not a real time OS.

The best you could do is run your method in a thread and "monitor" it from
another he only way for you to monitor it is using a Timer. when the time
elapse you can check for the status of the thread and act depending of its
status.

But the timer can (and most probably be) not precise. SO you cannot be sure
it will be exactly 1000ms

"markon" <ma****@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
Hi,

I would like to run few lines of code and limit the time. If the
procedure would not be finished say in 1000ms I would like to stop
processing it.

Unfortunately I do not know where to begin to find resources how to do
it.

Thanks

--
Martin

Mar 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phillip Parr | last post by:
Is there any way I can impose a time limit on the file() function, say if a file doesn't open within a second I can skip opening it? Thanks...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
2
by: User | last post by:
Anyone know of a function like raw_input(), but with a built-in or user-specified time limit? Ideally, it would return an empty string ('') and resume processing if the user didn't enter something...
24
by: Chen Shusheng | last post by:
Hello, I want to write a time delay function like "Timedelay(float time_lenth){}". When execute it, it will delay some seconds as long as "time_lenth" indicating. Could you help on how to write...
0
by: mdanesh | last post by:
I have a VBA macro code that runs a SQL query (that is coppied from MS ACCESS) and it is using record set definition in VBA. It works fine, but it times out before it is done. I believe it is...
9
by: jamesmcd999 | last post by:
I am currently developing a site and noticed that after about 2 minutes, I am being logged out of the members area. I have been trying to find a function that will allow me to specify the time...
1
by: woony | last post by:
I am making a webpage that gets his information from an as400 system. Now I seemed to hit some weird problem. I have for example this select: select field1,field2 from table this is over...
6
by: Rafe | last post by:
I tried to post some follow-ups to some issues I posted in the hopes of helping others, but I only get "reply to author" and "forward", but no "reply" option (using GoogleGroups). Is there some...
3
by: harita12 | last post by:
Hi I'm new to c# programming language, I'm making an app regarding online exam through paging, in which I want to show the time limit for the user and it decrease as the time passes & goes to next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.