Click here to Skip to main content
14,084,501 members
Rate this:
 
Please Sign up or sign in to vote.
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 2 days ago
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Yes; use a concurrent queue and a timer.

C++ Concurrent Queues[^]

A Simple Timer in C++ - Fluent C++[^]
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use the simple SetTimer API of Windows.

Tip: use PostThreadMessage to avoid GUI problems.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 16 May 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100