Run function every 2 milliseconds - vb.net

I am trying to run a function every 2 milliseconds but setting a timer to 2 milliseconds its not working it looks like it works every 50 milliseconds or so.. and when I try to use a While loop with Date.UtcNow.Ticks to compare 2 milliseconds then the CPU goes high. What options do I have here?

Depending on OS and Hardware configuration, system will allocate time slot to each process/thread,
You can try with on threading which will run full time and put statement thread.sleep(2); for delay and then run your code again in infinite while loop.

Related

How can I have the total time Psychopy

I made an experimentation on Psychopy. I have Intructions, 10 differents routines and then msg end.
I am able to have all the time it took for each routine, but I will like to have the total time of my 10 routines without having to calculate it my self in my csv file at the end.
Code for having my duration of each trial. I putted this line in each routine.
thisExp.addData('trial_duration1', t)
I tried to create a variable total and adding all the trial_duration, but my column was empty in the csv file at the end.
Thanks!!
Psychopy has an internal clock which starts when the experiment starts. You can read the time using core.monotonicClock.getTime(). The timing of this clock starts almost immediately as you hit "run", i.e. before the dialogue box, so it doesn't read the time since the first routine started. However, you can get that duration by first recording the time of the clock in a code component when you want time zero to be defined:
time_zero = core.monotonicClock.getTime()
... and then record the time elapsed doing
thisExp.addData('cumulative_duration', core.monotonicClock.getTime() - time_zero)
Note that if you want to do this only for particular loops (e.g. define time_zero in the first loop and record cumulative_duration in the last loop) require the condition to be satisfied:
# If this is the first iteration of the loop (no matter the name of the loop)
if currentLoop.thisN == 0:
time_zero = core.monotonicClock.getTime()

How to run CFRunLoop() for certain amount of time, then stop?

I currently have some code that requires running CFRunLoopRun().
This runs indefinitely. I would like to replace it with something that only runs for a set amount of time, say 30 seconds.
I tried CFRunLoopRunInMode(), but it exits immediately.
CFRunLoopRun(); // Works but never stops, I need to stop after 30s
CFStringRef mode = (__bridge CFStringRef)#"mode";
CFTimeInterval timeInterval = 10.0;
CFRunLoopRunInMode(mode, timeInterval, FALSE); // Doesn't work, syntax is wrong?, stops immediately
You are using a custom mode. It looks like you're just making it up for this one call. That would suggest that there are no input sources scheduled in that mode. All of the run-loop run functions exit immediately if there are no input sources.
CFRunLoopRunInMode() actually returns a value indicating why it exited. You should examine that.
The difference with CFRunLoopRun() is that it runs the run loop in the default mode (kCFRunLoopDefaultMode), not your custom mode. That mode almost certainly does have input sources scheduled (at least, assuming this is the main thread and thus the main run loop).
So, you could do this:
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, FALSE);
All of that said, running the run loop for a fixed time period is rarely the right approach. What are you actually trying to achieve? What led you to conclude that you need to run the run loop for 10 seconds? Why not just return to the normal event loop and use a timer to do some work in 10 seconds?

How to pause objective-c for an time less then an second

I am writing an app in Xcode 5.1.1 and objective-c.
I want to pause the program for 0.1 second.
To pause an program I normally use something like this:
sleep(1);
But "sleep" only takes an integer.
Question: is there an equivalent of sleep which can take floats?
Use following code. Convert your time to micro seconds.
usleep(100000); //Sleep Time 0.1 seconds
usleep(1000000);//Sleep Time 1 second.
or
[NSThread sleepForTimeInterval:0.1f];

Unstable NSTimer causes fluctuations in counting

I use NSTimer to count from a certain moment.
int totalSeconds;
int totalMinutes;
int totalHours;
If the totalSeconds are 60, totalMinuts become +1. Its very simple and should work.
For example i started the NSTimer together with the clock of my mac. (running on simulator).
When i look at the clock of my mac and the timer and compare the time the first 10-20 seconds its counting perfectly synchronous. After that it fluctuates or goes ahead 5 seconds or more.
So i output my timer and found this:
2012-10-24 14:45:44.002 xxApp driveTime: 0:0:44
2012-10-24 14:45:45.002 xxApp driveTime: 0:0:45
2012-10-24 14:45:45.813 xxApp driveTime: 0:0:46
2012-10-24 14:45:46.002 xxApp driveTime: 0:0:47
The milliseconds are timed at 002 as you see. But at the third row its 813. This happens very randomly and causes the fluctuations.
Is there a more stable way to count?
From the NSTimer documentation
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 your goal is to compute the total time that has passed since your program has started running, this is quite easy. As soon as you want to begin keeping track of time, store -[NSDate date] into a variable. Whenever you want to compute how much time has passed, call -[NSDate date again and do the following, assuming originalDate is a property where you stored the result of the first call to -[NSDate date]:
NSDate *presentDate = [NSDate date];
NSTimeInterval runningTime = [presentDate timeIntervalSinceDate:originalDate];
runningTime will be the total number of seconds that have elapsed since you started keeping track of time. In order to get the number of hours, minutes, seconds, and so on, an NSDateComponents object should be used.
This mechanism will allow you to use a timer to update your total running time "just about once a second" without losing accuracy.

How to stop a function call if its take more than 3 secs in Object c

i have a function it will take 3 to 30 secs time for execution depends on some calculations.
i want to stop if my function call takes more than 5 secs.
how to do this in Objective C.
You have to execute command in a separate thread (with performSelectorInBackground or with NSThread, for example), wait for 5 seconds (again, with unix sleep or NSThread methods) and then (depending on what is being done in the execution thread):
set some field in a class to "terminate", and check this field often in a "long" function
cancel IO operation, if there is a block
cancel a thread (you can read about it here: how to stop nsthread)
You can use a timer and put a condition like if the timer exceeds 5 seconds, do nothing.