|
|
Home » U++ Library support » U++ Callbacks and Timers » TimerCallBack interval resolution
TimerCallBack interval resolution [message #58017] |
Wed, 19 January 2022 07:41  |
 |
deep
Messages: 267 Registered: July 2011 Location: Bangalore
|
Experienced Member |
|
|
Hi
I am experimenting with TimerCallBack. I want to set callback interval to say 25 ms.
To check this I have done small code sample. It is attached to this message.
I am measuring delay with GetTickCount(). Running this for 10 sec. And counting - Histogram.
My observation shows time aligns to 20 ms
Timer Delay -20
Time Count
20 320
21 160
22 5
23 2
28 1
37 1
Timer Delay -25
Time Count
28 1
30 1
34 1
40 102
41 142
Timer Delay -19
Time Count
19 2
20 331
21 147
22 5
23 1
24 1
31 1
60 1
Timer Delay -22
Time Count
22 3
23 2
24 5
40 116
41 113
43 1
Timer Delay -38
Time Count
40 114
41 131
55 1
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct TimerCallBackVariation : TopWindow
{
Vector<int> interval;
dword prev, now;
TimeCallback timer, process;
int timerDelay, processDelay;
LineEdit le1;
String Result;
void Timer()
{
now = GetTickCount();
interval.Add ( now - prev );
prev = now ;
}
void Process()
{
timer.Kill();
Result << "Count - " << interval.GetCount() << "\n";
Result << "Interval Vector - " << interval << "\n" ;
Sort ( interval );
Result << "Sorted - " << interval << "\n";
VectorMap<int, int> hist;
int j, k;
for ( int i = 0; i < interval.GetCount(); i++ )
{
j = interval[i];
k = hist.Find ( j );
if ( k > -1 )
{
hist[k] = hist[k] + 1;
}
else
{
hist.Add ( j, 1 );
}
}
Result << "\nTimer Delay " << timerDelay << "\n";
Result << "Time \tCount" "\n";
for ( int i = 0; i < hist.GetCount(); i++ )
{
Result << hist.GetKey ( i ) << " \t" << hist.GetValues() [i] << "\n";
}
this->Title ( "Done" );
le1.Paste(Result.ToWString());
}
TimerCallBackVariation()
{
timerDelay = -38;
processDelay = 10000;
Title ( "Timer Callback Time Variation" ).Zoomable().Sizeable();
Add(le1);
le1.SizePos();
prev = GetTickCount();
timer.Set ( timerDelay, [=] {Timer();} );
process.Set ( processDelay, [=] { Process();} );
}
};
GUI_APP_MAIN
{
TimerCallBackVariation app;
app.Run();
}
Warm Regards
Deepak
|
|
|
Re: TimerCallBack interval resolution [message #58018 is a reply to message #58017] |
Wed, 19 January 2022 13:19   |
Tom1
Messages: 1301 Registered: March 2007
|
Ultimate Contributor |
|
|
Hi Deepak,
Are you running on Windows? If so, you can improve your timing to one millisecond resolution by adding this in your program:
#ifdef _WIN32
#include <MMSystem.h>
INITBLOCK{
timeBeginPeriod(1);
}
EXITBLOCK{
timeEndPeriod(1);
}
#endif
You may also want to use usecs() for measuring time at better than millisecond resolution.
Best regards,
Tom
[Updated on: Wed, 19 January 2022 13:22] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
Re: TimerCallBack interval resolution [message #59484 is a reply to message #58047] |
Wed, 04 January 2023 20:19   |
superdev
Messages: 31 Registered: August 2022
|
Member |
|
|
mirek wrote on Mon, 24 January 2022 10:47deep wrote on Mon, 24 January 2022 09:56Hi Mirek,
Thanks for explanation.
I was under impression that SetTimeCallback/TimeCallback as independent time trigger.
mirek wrote on Sun, 23 January 2022 00:02
It is supposed to be about 20ms by design and it is not even guaranteed. If you need more precise timing, you need to run it in separate thread.
Small code snippet or link will be helpful to setup repeat callback independent of GUI.
Frankly, the only time I did that was in SDL2 based sound synthetiser and there the period is set by SDL2 sound system.
Still, you can check it here:
https://github.com/ultimatepp/ultimatepp/blob/master/example s/Synth/Core.cpp
(or in examples/SDLSoundDemo)
So how to combine UPP GUI(Ctrl) and SDL2 window (>100 FPS)?
I've tried TopWindow::OpenMain(), then ProcessEvents and render in loop. It works but the loop freezes, for example during TopWindow resizing, dialog showing.
Win7x64, upp-win-17799
[Updated on: Wed, 04 January 2023 20:25] Report message to a moderator
|
|
|
Re: TimerCallBack interval resolution [message #59489 is a reply to message #59484] |
Thu, 05 January 2023 09:29   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
superdev wrote on Wed, 04 January 2023 20:19mirek wrote on Mon, 24 January 2022 10:47deep wrote on Mon, 24 January 2022 09:56Hi Mirek,
Thanks for explanation.
I was under impression that SetTimeCallback/TimeCallback as independent time trigger.
mirek wrote on Sun, 23 January 2022 00:02
It is supposed to be about 20ms by design and it is not even guaranteed. If you need more precise timing, you need to run it in separate thread.
Small code snippet or link will be helpful to setup repeat callback independent of GUI.
Frankly, the only time I did that was in SDL2 based sound synthetiser and there the period is set by SDL2 sound system.
Still, you can check it here:
https://github.com/ultimatepp/ultimatepp/blob/master/example s/Synth/Core.cpp
(or in examples/SDLSoundDemo)
So how to combine UPP GUI(Ctrl) and SDL2 window (>100 FPS)?
I've tried TopWindow::OpenMain(), then ProcessEvents and render in loop. It works but the loop freezes, for example during TopWindow resizing, dialog showing.
If you are doing a game, probably consider using VirtualGui. Otherwise, what about separate thread for SDL?
|
|
|
Re: TimerCallBack interval resolution [message #59497 is a reply to message #59489] |
Fri, 06 January 2023 16:20   |
superdev
Messages: 31 Registered: August 2022
|
Member |
|
|
mirek wrote on Thu, 05 January 2023 09:29superdev wrote on Wed, 04 January 2023 20:19...
So how to combine UPP GUI(Ctrl) and SDL2 window (>100 FPS)?
I've tried TopWindow::OpenMain(), then ProcessEvents and render in loop. It works but the loop freezes, for example during TopWindow resizing, dialog showing.
If you are doing a game, probably consider using VirtualGui. Otherwise, what about separate thread for SDL?
Video player.
What widgets can i use with VirtualGui? Sorry but its description is poor and i can't test it yet. SDL2Uword (built with fresh 16660, 32 or 64 bit) causes "exception c0000005 at 0".
"SDL render functions must be called from the main thread". I tried separate thread and it was glitchy.
Win7x64, upp-win-17799
[Updated on: Fri, 06 January 2023 16:28] Report message to a moderator
|
|
|
Re: TimerCallBack interval resolution [message #59498 is a reply to message #59497] |
Sat, 07 January 2023 09:10   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
superdev wrote on Fri, 06 January 2023 16:20mirek wrote on Thu, 05 January 2023 09:29superdev wrote on Wed, 04 January 2023 20:19...
So how to combine UPP GUI(Ctrl) and SDL2 window (>100 FPS)?
I've tried TopWindow::OpenMain(), then ProcessEvents and render in loop. It works but the loop freezes, for example during TopWindow resizing, dialog showing.
If you are doing a game, probably consider using VirtualGui. Otherwise, what about separate thread for SDL?
Video player.
What widgets can i use with VirtualGui?
All of them. The major disadvantage is that VirtualGui takes over window management, so that means it cannot really be used e.g. for normal applications.
What kind of application are you trying to develop?
|
|
|
Re: TimerCallBack interval resolution [message #59499 is a reply to message #59498] |
Sat, 07 January 2023 11:05   |
superdev
Messages: 31 Registered: August 2022
|
Member |
|
|
mirek wrote on Sat, 07 January 2023 09:10superdev wrote on Fri, 06 January 2023 16:20...
Video player.
What widgets can i use with VirtualGui?
All of them. The major disadvantage is that VirtualGui takes over window management, so that means it cannot really be used e.g. for normal applications.
What kind of application are you trying to develop?
Video player.
Win7x64, upp-win-17799
|
|
|
|
Re: TimerCallBack interval resolution [message #59501 is a reply to message #59500] |
Sun, 08 January 2023 06:38   |
superdev
Messages: 31 Registered: August 2022
|
Member |
|
|
mirek wrote on Sat, 07 January 2023 13:15superdev wrote on Sat, 07 January 2023 11:05...
Video player.
Interesting!
So that would imply that you need one window area capable of updating much faster than 40ms, while you want to keep GUI.
I guess you will need to go a bit deeper for this one. Probably the part handling videooutput running in separate thread, then you have to connect with it through low-level stuff, like HWND in Windows (and that part will be host specific).
No, i simply use SDL window for video and TopWindow for GUI
Win7x64, upp-win-17799
|
|
|
Re: TimerCallBack interval resolution [message #59512 is a reply to message #59498] |
Tue, 10 January 2023 19:12   |
superdev
Messages: 31 Registered: August 2022
|
Member |
|
|
mirek wrote on Sat, 07 January 2023 09:10superdev wrote on Fri, 06 January 2023 16:20..
Video player.
What widgets can i use with VirtualGui?
All of them. The major disadvantage is that VirtualGui takes over window management, so that means it cannot really be used e.g. for normal applications.
So what things cannot be done with VirtualGui?
Win7x64, upp-win-17799
|
|
|
|
|
Goto Forum:
Current Time: Mon Apr 28 18:13:39 CEST 2025
Total time taken to generate the page: 0.00766 seconds
|
|
|