Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Callbacks and Timers » TimerCallBack interval resolution
TimerCallBack interval resolution [message #58017] Wed, 19 January 2022 07:41 Go to next message
deep is currently offline  deep
Messages: 263
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 Go to previous messageGo to next message
Tom1
Messages: 1212
Registered: March 2007
Senior 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 #58019 is a reply to message #58018] Wed, 19 January 2022 14:27 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 263
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Tom

Thanks for reply.

I want this on Linux.

I need consistency in TimerCallback. Actually consistancy is there but with multiple of 20 ms interval.

My observation is I get time resolution of 20 ms.

I need to have say every 30 ms. +/- 2 ms callback. Is it possible with TimerCallback ?


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58020 is a reply to message #58019] Wed, 19 January 2022 14:40 Go to previous messageGo to next message
Tom1
Messages: 1212
Registered: March 2007
Senior Contributor
Hi,

I'm afraid I cannot help you there. You need someone with better Linux timing knowledge...

Best regards,

Tom
Re: TimerCallBack interval resolution [message #58028 is a reply to message #58020] Thu, 20 January 2022 21:43 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hello Deep,

You should look at RTIMING.
There is an example : upp/reference/Timing

Works on windows and linux: it should do the job Wink
Re: TimerCallBack interval resolution [message #58030 is a reply to message #58017] Fri, 21 January 2022 07:39 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 263
Registered: July 2011
Location: Bangalore
Experienced Member
Hi Didier,

My issue is not about time profiling.

I am looking at time interval and repeatability of time interval.

When I set TimerCallback time value between -22 to -39 ms. I get interval of 40 ms.
Depending on value of time I set I get time interval in multiple of 20 ms.

If I want 30 ms. time interval for timercallback I am not getting it. I get greater time interval - multiple time interval of 20 ms.


Interval repeatability is 1ms. which is okay for me.


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58036 is a reply to message #58030] Sat, 22 January 2022 19:32 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
deep wrote on Fri, 21 January 2022 07:39
Hi Didier,

My issue is not about time profiling.

I am looking at time interval and repeatability of time interval.

When I set TimerCallback time value between -22 to -39 ms. I get interval of 40 ms.
Depending on value of time I set I get time interval in multiple of 20 ms.

If I want 30 ms. time interval for timercallback I am not getting it. I get greater time interval - multiple time interval of 20 ms.


Interval repeatability is 1ms. which is okay for me.


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.

More details: GUI timer is run each time after the set of GUI events is processed and queue becomes empty. Those 20ms actually apply for the case that there are no events - if there are no event for 20 ms, timer is invoked. That also means if the GUI event (e.g. keystroke) takes more than 20ms to process, the interval can be actually much bigger than 20ms.
Re: TimerCallBack interval resolution [message #58046 is a reply to message #58036] Mon, 24 January 2022 09:56 Go to previous messageGo to next message
deep is currently offline  deep
Messages: 263
Registered: July 2011
Location: Bangalore
Experienced Member
Hi 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.


Warm Regards

Deepak
Re: TimerCallBack interval resolution [message #58047 is a reply to message #58046] Mon, 24 January 2022 10:47 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
deep wrote on Mon, 24 January 2022 09:56
Hi 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)
Re: TimerCallBack interval resolution [message #59484 is a reply to message #58047] Wed, 04 January 2023 20:19 Go to previous messageGo to next message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Mon, 24 January 2022 10:47
deep wrote on Mon, 24 January 2022 09:56
Hi 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-17059

[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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
superdev wrote on Wed, 04 January 2023 20:19
mirek wrote on Mon, 24 January 2022 10:47
deep wrote on Mon, 24 January 2022 09:56
Hi 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 Go to previous messageGo to next message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Thu, 05 January 2023 09:29
superdev 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-17059

[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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
superdev wrote on Fri, 06 January 2023 16:20
mirek wrote on Thu, 05 January 2023 09:29
superdev 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 Go to previous messageGo to next message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Sat, 07 January 2023 09:10
superdev 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-17059
Re: TimerCallBack interval resolution [message #59500 is a reply to message #59499] Sat, 07 January 2023 13:15 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
superdev wrote on Sat, 07 January 2023 11:05
mirek wrote on Sat, 07 January 2023 09:10
superdev 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.


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).

Re: TimerCallBack interval resolution [message #59501 is a reply to message #59500] Sun, 08 January 2023 06:38 Go to previous messageGo to next message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Sat, 07 January 2023 13:15
superdev 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-17059
Re: TimerCallBack interval resolution [message #59512 is a reply to message #59498] Tue, 10 January 2023 19:12 Go to previous messageGo to next message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Sat, 07 January 2023 09:10
superdev 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-17059
Re: TimerCallBack interval resolution [message #59513 is a reply to message #59512] Wed, 11 January 2023 10:36 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
superdev wrote on Tue, 10 January 2023 19:12
mirek wrote on Sat, 07 January 2023 09:10
superdev 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?


Management of normal windows.

Drag&Drop between VirtualGui app and host GUI.

Ditto clipboard.

Basically, it runs as separate GUI system, not integrated with rest of OS. OK for games / special uses. Bad for applications.
Re: TimerCallBack interval resolution [message #59516 is a reply to message #59513] Wed, 11 January 2023 18:17 Go to previous message
superdev is currently offline  superdev
Messages: 20
Registered: August 2022
Promising Member
mirek wrote on Wed, 11 January 2023 10:36
...

Thanks for the explanation.
Pardon my offtop.


Win7x64, upp-win-17059
Previous Topic: how to do to stop progress zip file ?
Goto Forum:
  


Current Time: Fri Mar 29 00:28:19 CET 2024

Total time taken to generate the page: 0.01515 seconds