How to terminate code that takes long in vb.net? - vb.net

How to terminate function/code (not entire page) when it takes some time, for example, more than 1 sec?
If Code > 1 Sec Then
Terminate the code....
I found the command "Server.ScriptTimeou", but it stops the entire page instead of one command.

You can run your function in a background process and start a timer simultaneously. Then abort the process if it runs more than 1 second.
If you want to run in the foreground then you probably have a loop somewhere that is taking a long time. Before you start running, save the current time. Then, somewhere in the middle of the loop, compare the saved time to the current time. When it hits one second, break out of the loop.

The solution will depend on what you're doing. If you're calling a single function that you have no control over and that can sometimes run longer than 1 second or so, you're pretty much stuck with having to run that function on a background thread and then terminate the thread if it runs long.
If you're actually running a long loop, or some other code that you DO have control over, you could just note the current time before starting the process, and in the loop, check if you've run long, and if so, exit the loop.
Just depends.

Related

Why Action In console was Paused till the process is done

When I use the python or etc,
In using for'for' command,
I print the row count
Beacause it can help to expect how long does it takes time,
But In VBA, Printing line numbers in console is paused when it reach some levels
However, the process is still operating normally.
when it process is done,
suddenly all output messages are displayed on the console at once.
The performance of the PC is very good. ( i7 CPU / ram = 16gb)
Why happen like this?
I used below code,
Please refer to this code and capture.
for RowVarialbe = 2 to 100000
debug.print (RowVariable)
next RowVarialbe
I don't know why, but I have observed the same. Add a
DoEvents
inside the loop and you'll be fine. It'll slow down the process slightly, so you might want to do it every nth row instead (but if performance is an issue, you shouldn't use the console that much - it is a performance killer). Another bonus is that the DoEvents allows you to pause or halt the execution.

Labview: Stop a While Loop On Completion of a VI Outside of Loop

I am calling the System Exec VI and in parallel would like a while loop to do a task repeatedly until the System Exec VI finishes, but I have not found a way for a while loop to either start before receiving all inputs, or be able to change an input's value after execution (through shift registers, etc) correctly for what I am trying to accomplish.
There are many options for communicating between parallel sections of code. My suggestion would be either a notifier (and you do your repetitive task when the wait times out) or an event structure (same idea - you do the repetitive task in the timeout event and you trigger the completion event with the data you get back from the System Exec VI, which then also stops the loop).
Note that in any case, the System Exec VI will only give you the output after it was done, so there's no way of knowing how much progress was made, unless your repetitive task involves looking at the number of files.

timeout on ReadLine from StandardOutput stream of process vb.net 2012

I have a process running. It should only take 1.5 minutes to run.. but sometimes something happens so at ReadLine it will get stuck. I am processing a video file.. I need a way to kill this process if it goes beyond the 3 min mark. I already have the condition handled if the process is killed.. I thought I could run a timer and after 3 minutes kill the process but ReadLine locks the thread.. this function is running in a thread on my main program..
If you want to run a timer it should be done on a second thread. a Console.ReadLine() will lock up your single thread.
using System.Threading.Timer
This is what I used.. Thanks colton for pointing my the right direction
Dim tmr As New System.Threading.Timer(AddressOf MyTimerTick)
tmr.Change(180000, 0)

How to fix major lag in NSTimers?

In a fairly simple application that I am making, I use many NSTimers, one which runs at a rate of .01 seconds that I use to change the position of multiple images. This causes major lag. How can I fix this? Please explain in detail, as I am fairly new to app dev.
From the NSTimer Docs (emphasis: mine):
A timer is not a real-time mechanism; it fires only when one of the
run loop modes to which the timer has been added is running and able
to check if the timer’s firing time has passed. Because of the various
input sources a typical run loop manages, the effective resolution of
the time interval for a timer is limited to on the order of 50-100
milliseconds. If a timer’s firing time occurs during a long callout or
while the run loop is in a mode that is not monitoring the timer, the
timer does not fire until the next time the run loop checks the timer.
Therefore, the actual time at which the timer fires potentially can be
a significant period of time after the scheduled firing time.
If you want to work at the display frequency, see CADisplayLink.
However, you should first understand where you program spends its time now to understand what makes it slow (profiler).

timing a method's duration

I have a splash that displays at my app's startup while it loads data from a Web query. I'd like to make sure the splash displays for at least 3 seconds, but sometimes the load query takes less than this. However, I don't want this 3 seconds added to the time it takes to load the web data; I just want to make sure the minimum is 3 seconds.
In other words, can I time how long it takes to load the data, then maybe set a delay for the difference between that load time and 3 seconds, and make the splash hang on (using a selctor or something) for that extra amount of time?
I'd suggest that instead of doing it that way, you start a 3 second long timer when you start the load query. When each finishes, set a boolean to say that one finished and call a method that checks if both are done yet; if so, it's time to close the splash window. (sorry, no code examples - I'm not familiar with Objective C)