Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i have a lot of code in one place in my program i what to do the code for x time
for example:

VB
code
..
 time(10000) ' 10 sec
  ...
   'some code
  ...
 End time

'rest of the code.


Whats the best way to do it.

Thank's
Posted
Updated 20-Apr-15 1:52am
v3
Comments
Sergey Alexandrovich Kryukov 9-Apr-15 2:01am    
So far, it does not look an reasonable, but...
What would you like to achieve with that? What should happen to the incompletely processed data?
—SA
[no name] 9-Apr-15 2:24am    
its was example the thing i want to do is simple timeout for any code invoke
(not only data receive)
Sergey Alexandrovich Kryukov 9-Apr-15 2:36am    
This is pretty easy to achieve, but what to do with data? It could be lost. The answer depends on how do you want to preserve results of your work when timeout happens. And why?...
—SA
[no name] 9-Apr-15 2:54am    
for example after timeout if data is not complete do nothing.
Sergey Alexandrovich Kryukov 9-Apr-15 2:59am    
Define "do nothing". If you, say, collected some data in a file, and through out without closing it, do you thing that it will hold previously written data? Think again. Some or all of data will be lost, depending on how you do it. This way, "do nothing" will actually destroy data. A bit tricky? But this is true.
—SA

1 solution

Probably, you are going about this the wrong way: instead of trying to limit the amount of time that your code segment can run (which is not going to be simple, depending on exactly what you are doing, and how you are doing it) you might want to consider moving the long running code into a separate thread and letting it run continuously.

I would assume from your pseudocode that you are recording date from some source, and only want to display a small amount of it. If so, them moving the data receive process into a different thread would probably be a better solution.

Have a look at the BackgroundWorker class for a simple way to do this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900