Free Source Code and Program Tips
Intermittent work in UI thread
- Pedro Ferreira
I’m trying to create a UI thread to do constant background work, but I’m having some design problems.
The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be controlled with thread messages (to start, pause, query…).
My problem is where should I put the processing work. I need it to be responsive between cycles, where the process waits for 1 minute, so I can stop it in that period. My only idea is using OnIdle to do the work.
But then, how should I make it real idle when waiting for 1 minute?
Could anyone give some hints on what is the way to go?
- Scott McPhillips [VC++ MVP]
You can’t use Sleep and still be responsive to messages. Instead of using Sleep you could use SetTimer in the main thread and post a message to the thread, or SetTimer to a hidden window within the thread, or you could use a loop within the thread and use MsgWaitForMultipleObjects within the loop.
MsgWaitForMultipleObjects has a timeout parameter that gives you the sleep period, but it also returns early if a message or event comes along.
- Almond and Joseph M. Newcomer [MVP]
I would be strongly disinclined to use MsgWaitForMultipleObjects. You don’t gain anything by using it over using WM_TIMER, and it requires that you have to write your own message pump, always a dangerous proposition.
- BUT, SOME MORE DISCUSSION ABOUT THE UI THREAD AND WORKER THREAD…
- Almond
Do not use UI threads for this kind of thing.
- David
I would create a worker thread (not a UI thread) unless you need a UI thread for some specific purpose not apparent in this description. The worker thread would do it’s work and then do a WaitForSingleObject() with a timeout of 1 minute (60000 ms), which causes WaitForSingleObject() to return when either the main thread sets an event for starting, pausing, querying, etc. or the 1 minute has elapsed. If you need separate events each for starting, pausing, querying, etc. use WaitForMultipleObjects() instead of WaitForSingleObject().
- Joseph M. Newcomer [MVP]
UI threads are actually ideal for this sort of thing. Don’t be confused by the phrase ‘UI’ meaning something with windows; think of it as “a thread with a built-in queuing mechanism”. They’re really very good for things that require timed events as the OP described.
There are some advantages to UI threads; for example, a UI thread has a built-in queue.
Also, for some kinds of interfacing require a message pump to deal with event handling. I don’t do databases so I don’t know if databases have this requirement, but there are On-Anything handlers involved in the database interface, then a UI thread is required.
| Print article | This entry was posted by support on October 16, 2007 at 4:27 pm, and is filed under Win32/MFC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No comments yet.
No trackbacks yet.
How to catch the events when click on the app icon on taskbar
about 1 year ago - No comments
How to catch the events when you click on the application’s icon on the taskbar? It’s the WM_NC class messages. For example, you can handle the WM_NCLBUTTONDBLCLK message. And you can alse reponsible the WM_SYSCOMMAND messages. In particular, the special cases of SC_MINIMIZE and SC_MAXIMIZE are what are called from the system menu, which is
The Simplest way to change the row height of CListCtrl
about 2 years ago - No comments
In the post “How to change the row height of CListCtrl“, it gives 2 solution example how to change row height or CListCtrl. Hower, it is not the simplest way. The simplest way to change the row height in a CListCtrl is: Display an icon with the desired height. All other ways that I know
How to Adding CTreeCtrl in CListCtrl
about 2 years ago - No comments
In some case, you need such a control as the following figure: It’s a CListCtrl like control, but it binds a CTreeCtrl in the first column. But you can also consider it as a CTreeCtrl and each of the items has the list style. All roads lead to Rome. But in 1999, David Lantsman has
How to send/post message to CDocument?
about 2 years ago - 2 comments
Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can’t receive message. How do that? There are 3 solutions: Solution 1: Send the message to the main window. Send/Post the message to the main frame or the view, and then call some functions of the document. But if
How to use an derived CListCtrl in CListView
about 2 years ago - 2 comments
Sometime you use CListView and need to extend the functionality of the CListCtrl of the view. For example, you want to overwrite the handler function of the NM_CUSTOMDRAW or WM_SIZE message. So the problem is: How to tell CListView to use your own CListCtrl? OK, you are on the wrong way now. The CListView does
How to set the backgroud color of CPropertyPage
about 2 years ago - No comments
How to set the backgroud color of CPropertyPage? If you override OnCtlColor in your property page, then you can return a different background color and that seems to work with CStatic and other items. But what if you want is a background that’s not just a simple color? You’d like to be able to draw
How to select the item of ListView(CListCtrl)
about 2 years ago - No comments
Use CListCtrl::SetItemState with LVIS_SELECTED
Intermittent work in UI thread
about 2 years ago - No comments
Pedro Ferreira said: I’m trying to create a UI thread to do constant background work, but I’m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will
How to read sectors of a physical drive under Win32?
about 3 years ago - No comments
How to read the sectors of physical drive under win32? The API ReadFile is most useful function, and with it, you can read the physical sectors directly. Here is a sample function, BOOL ReadSectors(…) BOOL ReadSectors( BYTE bDrive, //drive index DWORD dwStartSector, //start sector WORD wSectors, //sectors number LPBYTE lpSectBuff) {  if (bDrive == 0)
How to get a fixed code for serial number(2)
about 3 years ago - No comments
I have post an article about “How to get a fixed code for serial number” at http://www.ucosoft.com/archives/51.html. In that article, I suggest you try WMI class to retrieve some hardware ID. But there is some limitation of this method. The WMI redist package is not always available on user’s system, for example, Windows NT4 and Windows