visual-c++comboboxmfc

Automatically updating a CComboBox


I have a CComboBox on my CDialog which has a list of file names.

The the folder where the files reside is sometimes updated. And this means:

Which is the best way to handle this?

  1. Using OnKickIdle.
  2. Handling the combo box OnDropDown event?

Thanks for guidance. I am concerned about performance impact on the application.


Solution

  • Well, each solution has its pros and cons. My thoughts:

    I wouldn't use OnKickIdle(), as it's rather "wasteful"; it's an event fired really "all the time", even as a result of mouse movement, hovering, pressing a key etc. Add a Beep(1000,30) command in OnKickIdle() to check how many times it is called, and ask yourself if you would like having your application scanning the directory that often. And you may trigger an update even when the drop-list is expanded. You could instead use some timer, but this is just the good old "Polling" technique.

    Scanning the folder in OnDropDown() has the obvious disadvantage of performing a relatively costly operation at the very moment when the user is awaiting the UI to respond. However if the folder resides in an SSD on the same machine and the number of files is small, response time can be absolutely acceptable. May be a different story if the number of files is large and/or the folder is on a network volume.

    If you are willing to put a little more work into this, you can consider Directory Change Notifications. It can be implemented in a separate thread, which will make the update procedure imperceptible to the user.


    EDIT:
    The documentation mentions that the handles returned by the FindFirst/FindNext notification functions must be waited on by using the wait functions. That is, the calling thread will be blocked until a notification is received (directory/files contents or attributed changed) or a timeout occurs (if a timeout other than INFINITE was used in the wait function). Of course you don't want your UI thread to be blocked, therefore a separate worker thread (to wait for the notifications) is the best configuration.

    An alternative could be using the MsgWaitForMultipleObjects() function, along with a custom message-loop, so that UI messages can be processed. I find this a lot more complicated and tricky, as you will have to meddle with the standard message-processing of MFC. Creating a new thread is far simpler and more clear.