Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i put some delay before updating the GUI thread from worker threads using PostMessage but not using sleep?

The receiving of events from sockets are completely out of sync.
Sometimes there are bursts in connections and there must be some sort of delay to avoid flooding the GUI thread.

What is the best way to achieve this in terms of performance?

What I have tried:

Put the data in some sort of queue list then let the GUI thread loops each x seconds and update itself but that requires lock so it would be thread-safe.
Posted
Updated 16-May-19 10:55am
v2

Yes; use a concurrent queue and a timer.

C++ Concurrent Queues[^]

A Simple Timer in C++ - Fluent C++[^]
 
Share this answer
 
Use the simple SetTimer API of Windows.

Tip: use PostThreadMessage to avoid GUI problems.
 
Share this answer
 
Personally, I seriously doubt that you could flood the GUI thread so heavily that it would be a bad thing.

If you really think you need to then I would queue the update messages in a FIFO and have the GUI unload them when you want it to. If nothing else, it will minimize blocking of the workers.
 
Share this answer
 
Comments
NoviceCoder87 17-May-19 5:05am    
The Listview control does not respond during the load of items. How can i relief the listview to be responsive during load of items? i only thought of delay. Did not find another fix for this.
Rick York 17-May-19 10:54am    
It can't unless you 'come up for air' between loading entries to it. I guess it's a matter of which do you consider most important. You can set a fast timer (1ms) and load one message per click and that will keep your app responsive but it will take longer or you can just load them all as fast as possible. If you load them all at once you can disable updates of the listview and that will speed things up even more.
NoviceCoder87 17-May-19 12:59pm    
Thanks for your response. The load of items is not done at one go, meaning, its not loaded all in a single loop but rather from messages posted from worker threads. The impact of this without any kind of delay between posted message can be noticed in the listview control as the right click on this listview control never shows the menu up only if all items loaded. Each PostMessage/PostThreadMessage (Worker->GUI) will cause a load of a single listview item (LVM_INSERTITEM). Yes you have good answer, i prefer the responsive more than caring about how fast to load them.
Rick York 17-May-19 13:20pm    
I can understand that. An unresponsive UI will be annoying to users. You could make the timer period adaptive by dropping the period as the queue gets full. For example, if you have 20 or more entries kill the timer and recreate it at 5mS or something like that and if it has less than 20 entries then set it to 100mS. These values are just WAGs. You should make them all configuration settings so you can adjust them to get the behavior you want without recompiling.
NoviceCoder87 18-May-19 0:28am    
Yes thanks Rick. Very good method of adaptive timer interval. Did not think of it. You have very good imagination. I will accept your answer as the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900