call a method at a given time

I have an app which is interrogating a measurement device at fixed times (eg every 15 seconds), aligned to minutes. Currently I am using a timer in single-shot mode, re-launched after the measurement activity. In the last period before the minute it adjusts the period so that it ands up exactly on the minute.

The problem is that sometimes, due to the interaction with the device, the measurement process takes more than 20 sec (and often with a run of such events), so that the timing gets completely out of sync.

A better solution would be to trigger the measurement event at fixed times by the clock, taking account that the device may not be available at the measurement time. This would at least keep things in sync.

Any ideas how to trigger events at fixed times without using a timer?

thanks,
Richard

[quote=496545:@Richard Francis]I have an app which is interrogating a measurement device at fixed times (eg every 15 seconds), aligned to minutes. Currently I am using a timer in single-shot mode, re-launched after the measurement activity. In the last period before the minute it adjusts the period so that it ands up exactly on the minute.

The problem is that sometimes, due to the interaction with the device, the measurement process takes more than 20 sec (and often with a run of such events), so that the timing gets completely out of sync.

A better solution would be to trigger the measurement event at fixed times by the clock, taking account that the device may not be available at the measurement time. This would at least keep things in sync.

Any ideas how to trigger events at fixed times without using a timer?

thanks,
Richard[/quote]
Could the timer not start a thread to run the measurement code? When the timer runs you could also check if the thread is already running and not run it again if it is.

Better just run a timer with 15000 milliseconds period.

with a thread you will get the exact timing, in the thread you can create a object (your class for a measurement ) that do the job,
put this object into a list and you can visualize it if the measurement had a result.

a thread in a window and a ClassMeasurement with super to Thread

[code]Sub Open() Handles Open
Thread1.Start

End Sub
[/code]

[code]Sub Run() Handles Run
Var t As DateTime = DateTime.Now
While 1=1

Var interval As DateInterval = DateTime.Now - t
If interval.Seconds = 15 Then
  System.DebugLog "Tick"
  Var o As New ClassMeasurement 
  o.Start
  List.AddRow o
  t = DateTime.Now
End If
Me.Sleep 500

Wend
End Sub
[/code]

Public Property List() as ClassMeasurement

Thanks Markus, I’m working on it. It’s a bit more complicated as the measurement means of sending a command via a TCP socket, which in turn triggers an event when there’s a response (or an error or a timeout). But I’m on it!

thanks,
Richard

I’m afraid this thread approach doesn’t look like it’s going to work. The interaction between the events raised by the socket and the rest of the app, which ultimately leads back to the GUI, seem to be incompatible with the way the thread operates.

If I could use a thread as you propose to raise events, which would then trigger actions in the main thread, that would be great. But the use of a thread with events seems to be not allowed.

So,

  • a timer with a 15000 millisec period is no good (it steadily loses time so that after a few hours it’s totally out of sync);
  • a retrigger of a single-shot 15000 millisec timer also loses sync (but is much better than a multiple 15000 ms timer);
  • the best I found so far is a single-shot timer reset every 15000 ms, with a calculated period at the end of every minute to bring it back to sync.

However, as I said originally, if I get a run of anomalous measurement events which each take more than 20 sec, then sync is lost completely.

I need a trchnique, not based on a thread, to generate an event at a given clock time.

cheers,
Richard

Not allowed in what way? You get a ThreadAccessingUIException ?

If that’s the case, then use the UserInterfaceUpdate event to queue actions which might cause that exception. I do such as:

AddHandler myThread.UserInterfaceUpdate, WeakAddressOf updateUI // Wire in the UI updater method for this thread

and then AddUserInterfaceUpdate to send requests to updateUI.

[quote=496739:@Richard Francis]I’m afraid this thread approach doesn’t look like it’s going to work. The interaction between the events raised by the socket and the rest of the app, which ultimately leads back to the GUI, seem to be incompatible with the way the thread operates.

If I could use a thread as you propose to raise events, which would then trigger actions in the main thread, that would be great. But the use of a thread with events seems to be not allowed.

So,

  • a timer with a 15000 millisec period is no good (it steadily loses time so that after a few hours it’s totally out of sync);
  • a retrigger of a single-shot 15000 millisec timer also loses sync (but is much better than a multiple 15000 ms timer);
  • the best I found so far is a single-shot timer reset every 15000 ms, with a calculated period at the end of every minute to bring it back to sync.

However, as I said originally, if I get a run of anomalous measurement events which each take more than 20 sec, then sync is lost completely.

I need a trchnique, not based on a thread, to generate an event at a given clock time.

cheers,
Richard[/quote]
What does the thread update in the gui?

If you need a high resolution timer, please check our TimerMBS class in MBS Xojo Win Plugin.
It can do 1ms periods on Windows, which the normal Xojo timer can’t do.