My scenario is to wait for a particular element. It might take 2,3,4,5 days.
Can that be achieved using explicit wait or fluent wait?
If not what can be the maximum time we can give for explicit wait or fluent wait?
Use below code :-
int MINUTES = 1; // The delay in minutes
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() { // Function runs every MINUTES minutes.
YOURFUNCTIONNAME();
}
}, 0, 1000 * 60 * MINUTES);
Add the time as per your requirement
Related
We want to code a very accurate and consistent timer interval (100ms) to generate a periodic light pattern with the phone tourchlight.
Currently we use a runnable function, but this shows a lot of deviation.
If we measure the response with a photodiode we perceive deviations between 80 and even 150ms!
Is this error caused by the runnable function or the execution of the enable/disable of the Tourch function.
Below you can find the runnable that we used to trigger the phone tourchlight. The on/off period should be 100ms but is inconsistant variation of lenght and variation between on/off period. could this have to do with the camera service?
private final Runnable mRunnable = new Runnable() {
public void run() {
if (mActive) {
if (mSwap) {
mSwap = false;
mHander.postDelayed(mRunnable, 100);
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
} else {
mSwap = true;
mHander.postDelayed(mRunnable, 100);
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
}
}
}
};
Please let us know if you need more information
I have a timer task as shown below:
long timerSec = 5000;
TimerTask task1 = new TimerTask()
{
public void run()
{
//Some code...
System.out.println("Timer task...");
}
};
And a timer object as shown below:
Timer readFileTimer = new Timer();
I scheduled a task with 5 secs period between two successive task executions.
readFileTimer.scheduleAtFixedRate(task1, 0, timerSec);
Below line of code assigns new time period. I want to change the time period from 5 secs to n-secs (lets say 10 secs w.r.t. timerSec value).
timerSec = CalculateTimeForUpgrade(); //Get new timer interval period.
I tried below code, but didn't get the expected result.
readFileTimer.scheduleAtFixedRate(task1, 0, timerSec);
Please help. Thanks in advance.
Instead of starting the task at a fixed interval from the beginning, reschedule the task every time you finish it.
Something like this:
final Timer readFileTimer = new Timer();
readFileTimer.schedule(new MyTimerTask(), 0);
.......
private class MyTimerTask extends TimerTask() {
#Override
public void run() {
// Some code...
System.out.println("Timer task...");
if (readFileTimer!=null)
readFileTimer.schedule(new MyTimerTask(), CalculateTimeForUpgrade());
}
}
I am trying to write a simple application on Linux in C. when a push button is press, a motor is started, after 20 seconds it is stop by a timer. apparently, I don't need a repeating timer, a one-shot timer is can do the job.
what I need to know is which timer api i can use set timeout and trigger my function when timer expires.
thanks a lot.
Finally I figured it out.
For my case, using alarm if sufficient.basically,you need to define you StartMotor() and stopMotor() function and attach StopMotor function to signal(SIGALRM, StopMotot);
RUN_DURATION 30 // 30 seconds
static bool MotorStarted = false;
static void StopMotor(int sig);
void main(void){
while(1)
{
if (IsPBPressed( )) //polling status of push button.
{
if(!MotorStarted) //if motor is running, preceed startmotor,
{ // otherwise, ignore this subsequent request.
MotorStarted = true;
startMotor()
signal(SIGALRM, StopMotor);// start a timer. motor stop when the timer expires.
alarm(RUN_DURATION);
}
}
} //IsPBPressed( )
}
Hy, I just wanted to find out how to implement a loop that takes around 80 to 120ms (mostly 80ms) to execute. The loop has to execute for about 30 minutes... basically it is a SURF matching algorithm.
Currently I am using a System.Threading.Timer to create a timer that executes after every 90ms, but the problem is that since the computation time is variable, so after some time the stack overflows and the program closes.
I'm using WPF to create the GUI.
Is there a better way to implement such a loop using threading? Any help would be much appreciated! Thanks in advance.
//initialization
private System.Threading.Timer Visual_AR1;
Visual_AR1 = new System.Threading.Timer(new TimerCallback(Video_AR1), null, 0, 90);
private void Video_AR1(object state)
{
lock (this)
{
// SURF matching
modelImage_AR1 = new Image<Gray, byte>(droneControl_AR1.BitmapImage).Resize(1.8, INTER.CV_INTER_NN);
map_image_d1 = DrawMatches_gpu.GPU_0(wayx, modelImage_AR1, observedImage, pgpuObservedKeyPoints_imp, pgpuObservedDescriptors_imp, out matchTime_0, out pX1, out pY1);
Dispatcher.BeginInvoke((Action)delegate()
{
label4.Content = "Time Taken by GPU_0 : " + matchTime_0.ToString();
});
mask_selector_d1();
}
}
is this a viable solution ?
private Thread threadTask = null;
private void threadTask_Start()
{
if (threadTask == null) {
threadTask = new Thread(SURF);
threadTask.Start();
}
}
private void SURF()
{
while(true)
{
lock (this)
{
// SURF matching
modelImage_AR1 = new Image<Gray, byte>(droneControl_AR1.BitmapImage).Resize(1.8, INTER.CV_INTER_NN);
map_image_d1 = DrawMatches_gpu.GPU_0(wayx, modelImage_AR1, observedImage, pgpuObservedKeyPoints_imp, pgpuObservedDescriptors_imp, out matchTime_0, out pX1, out pY1);
Dispatcher.BeginInvoke((Action)delegate()
{
label4.Content = "Time Taken by GPU_0 : " + matchTime_0.ToString();
});
mask_selector_d1();
}
thread.sleep(40);
}
}
Instead of making the timer tick perform the work, make the timer tick queue up a job and have something else (presumably threads from the thread pool) consume the jobs. You can use a semaphore to throttle the number of jobs running simultaneously and you can examine the state of the queue to avoid this sort of over-commit problem.
I want to play simultaneous multiply audio sources in Silverlight.
So I've created a prototype in Silverlight 4 that should play a two mp3 files containing the same ticks sound with an intervall 1 second. So these files must be sounded as one sound if they will be played together with any whole second offsets (0 and 1, 0 and 2, 1 and 1 seconds, etc.)
I my prototype I use two MediaElement (me and me2) objects.
DateTime startTime;
private void Play_Clicked(object sender, RoutedEventArgs e) {
me.SetSource(new FileStream(file1), FileMode.Open)));
me2.SetSource(new FileStream(file2), FileMode.Open)));
var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1) };
timer.Tick += RefreshData;
timer.Start();
}
First file should be played at 00:00 sec. and the second in 00:02 second.
void RefreshData(object sender, EventArgs e) {
if(me.CurrentState != MediaElementState.Playing) {
startTime = DateTime.Now;
me.Play();
return;
}
var elapsed = DateTime.Now - startTime;
if(me2.CurrentState != MediaElementState.Playing &&
elapsed >= TimeSpan.FromSeconds(2)) {
me2.Play();
((DispatcherTimer)sender).Stop();
}
}
The tracks played every time different and not simultaneous as they should (as one sound).
Addition:
I've tested a code from the Bobby's answer.
private void Play_Clicked(object sender, RoutedEventArgs e) {
me.SetSource(new FileStream(file1), FileMode.Open)));
me2.SetSource(new FileStream(file2), FileMode.Open)));
// This code plays well enough.
// me.Play();
// me2.Play();
// But adding the 2 second offset using the timer,
// they play no simultaneous.
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
timer.Tick += (source, arg) => {
me2.Play();
((DispatcherTimer)source).Stop();
};
timer.Start();
}
Is it possible to play them together using only one MediaElement or any implementation of MediaStreamSource that can play multiply sources?
Addition 2: Debug the playing positions
Adding the debug information shows definitively that the me plays different compare to the timer's ticks
...
me2.Play();
System.Diagnostics.Debug.WriteLine(
me.Position.TotalMilliseconds + " -> " + me2.Position.TotalMilliseconds);
// 1820 -> 0 (but not 2000 -> 0)
Addition3: Experience with markers
I have experienced with the Time in the TimeLineMarker and following code works well enough on my pc
me.Markers.Clear();
me.Markers.Add(new TimelineMarker { Time = TimeSpan.FromMilliseconds(1892) });
me.MarkerReached += (source, args) => {
me2.Play();
me.Markers.Clear();
};
me.Play();
Since you're using FileStreams to load the files, I assume you're not reading them over the web, but rather from the local file system.
You don't want to call Play() from the timer tick handlers if it's not necessary - ie for the first ME.
Also, you're running the timer every millisecond, not second (not sure if that's actually what you meant to say). If you want to kick of the second play 2 seconds after the first, then try calling me.Play() then create the timer to run on a 2-sec interval, and all it does is call me2.Play() and stop itself.
Also, in line with the comment above, to see if the behavior is reasonably deterministic (sufficiently synchronized), try running just this a few times to see if, in principle, they can play together well enough for you.
private void Play_Clicked(object sender, RoutedEventArgs e) {
me.SetSource(new FileStream(file1), FileMode.Open)));
me2.SetSource(new FileStream(file2), FileMode.Open)));
me.Play();
me2.Play();
}
.. Edit:
Timers are not guaranteed to execute
exactly when the time interval occurs,
but they are guaranteed to not execute
before the time interval occurs. This
is because DispatcherTimer operations
are placed on the Dispatcher queue
like other operations. When the
DispatcherTimer operation executes is
dependent on the other jobs in the
queue and their priorities. (msdn :
DispatcherTimer)
It can be very difficult to achieve perfect synchronization in multi-threaded contexts.. The DispatcherTimer is going to place the call to the handler in the UI Dispatcher when the interval ticks, but that doesn't mean it will get immediately invoked. The only thing that might be worth a shot at this point is to adjust the DispatcherPriority (e.g. to 'Send' (the highest)).
Do so with this constructor:
public DispatcherTimer(
DispatcherPriority priority
)