Correct Way to Update CProgressBar from Thread

I have a database program that currently updates the status bar while
working from another thread.

Currently the code it uses is a parameter passed to the thread with a
pointer to the application and:

pApp->m_ProgressCurrent.StepIt();

I've seen people talking about not touching dialogs from other threads,
and I was wondering what the proper method would be to have it refresh
the progress indicator.
The controls can range from steping once every 30 seconds (for a large
database) to 100+ times a second (for many small databases).

What would be the correct way to update a GUI control from the thread?

Thanks,
Josh McFarlane

0
darsant (187)
7/1/2005 4:29:16 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
1044 Views

Similar Articles

[PageSpeed] 7

"Josh McFarlane" <darsant@gmail.com> wrote in message
news:1120235356.672546.281080@z14g2000cwz.googlegroups.com...
> I have a database program that currently updates the status bar while
> working from another thread.
>
> Currently the code it uses is a parameter passed to the thread with a
> pointer to the application and:
>
> pApp->m_ProgressCurrent.StepIt();
>
> I've seen people talking about not touching dialogs from other threads,
> and I was wondering what the proper method would be to have it refresh
> the progress indicator.
> The controls can range from steping once every 30 seconds (for a large
> database) to 100+ times a second (for many small databases).
>
> What would be the correct way to update a GUI control from the thread?
>
> Thanks,
> Josh McFarlane
>

If the progress bar is owned by the main GUI thread, and not by the database
worker thread, then in the worker you might try posting a message directly
to the hwnd of the progress control.  Use the API's ::PostMessage function
and not MFC's CWnd::PostMessage.  Don't use SendMessage.

You also might want to throttle the messages a bit, since 100 messages a
second might swamp the message loop needlessly and make your GUI somewhat
unresponsive.  In other words, instead of PostMessage( hwnd,
PBM_STEPIT,...), you might want to determine when there's been a visible
change in the position of the progress control, and PostMessage( hwnd,
PBM_SETPOS,...) instead.  This would decrease the number of messages sent,
while still providing the user with good visual feedback.

One reason to avoid MFC's StepIt() function is that it wraps an API call to
SendMessage.  Usually avoid a SendMessage for inter-thread communication,
since the SendMessage function does not return until the message has been
handled, and that requires a thread-switch which slows your thread
considerably.  (It's as if you had a fictitious WaitForMessageToBeHandled
function in your thread.)  Did you notice that your thread did not seem to
process the database records as quickly as you would have expected?



0
7/1/2005 7:09:27 PM
Why would you need the API PostMessage and not MFC's PostMessage? MFC's PostMessage works
just fine.

The observation about throttling is important. In addition, you might want to check out my
essay on my MVP Tips site on the use of I/O Completion Ports as an interthread queueing
mechanism to eliminate the problems caused by PostMessage swamping the input queue.
					joe
On Fri, 1 Jul 2005 12:09:27 -0700, "Michael K. O'Neill" <MikeAThon2000@nospam.hotmail.com>
wrote:

>"Josh McFarlane" <darsant@gmail.com> wrote in message
>news:1120235356.672546.281080@z14g2000cwz.googlegroups.com...
>> I have a database program that currently updates the status bar while
>> working from another thread.
>>
>> Currently the code it uses is a parameter passed to the thread with a
>> pointer to the application and:
>>
>> pApp->m_ProgressCurrent.StepIt();
>>
>> I've seen people talking about not touching dialogs from other threads,
>> and I was wondering what the proper method would be to have it refresh
>> the progress indicator.
>> The controls can range from steping once every 30 seconds (for a large
>> database) to 100+ times a second (for many small databases).
>>
>> What would be the correct way to update a GUI control from the thread?
>>
>> Thanks,
>> Josh McFarlane
>>
>
>If the progress bar is owned by the main GUI thread, and not by the database
>worker thread, then in the worker you might try posting a message directly
>to the hwnd of the progress control.  Use the API's ::PostMessage function
>and not MFC's CWnd::PostMessage.  Don't use SendMessage.
>
>You also might want to throttle the messages a bit, since 100 messages a
>second might swamp the message loop needlessly and make your GUI somewhat
>unresponsive.  In other words, instead of PostMessage( hwnd,
>PBM_STEPIT,...), you might want to determine when there's been a visible
>change in the position of the progress control, and PostMessage( hwnd,
>PBM_SETPOS,...) instead.  This would decrease the number of messages sent,
>while still providing the user with good visual feedback.
>
>One reason to avoid MFC's StepIt() function is that it wraps an API call to
>SendMessage.  Usually avoid a SendMessage for inter-thread communication,
>since the SendMessage function does not return until the message has been
>handled, and that requires a thread-switch which slows your thread
>considerably.  (It's as if you had a fictitious WaitForMessageToBeHandled
>function in your thread.)  Did you notice that your thread did not seem to
>process the database records as quickly as you would have expected?
>
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
7/1/2005 8:45:36 PM
Josh McFarlane wrote:
> I have a database program that currently updates the status bar while
> working from another thread.
> 
> Currently the code it uses is a parameter passed to the thread with a
> pointer to the application and:
> 
> pApp->m_ProgressCurrent.StepIt();
> 
> I've seen people talking about not touching dialogs from other threads,
> and I was wondering what the proper method would be to have it refresh
> the progress indicator.
> The controls can range from steping once every 30 seconds (for a large
> database) to 100+ times a second (for many small databases).
> 
> What would be the correct way to update a GUI control from the thread?
> 
> Thanks,
> Josh McFarlane
> 

PostMessage to a window in the main thread.  There is a step by step 
example here:
http://www.mvps.org/vcfaq/mfc/12.htm

-- 
Scott McPhillips [VC++ MVP]

0
Scott
7/1/2005 11:04:54 PM
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:mmabc1th2ubumljk99m4e6uu0a85m2u5b0@4ax.com...
> Why would you need the API PostMessage and not MFC's PostMessage? MFC's
PostMessage works
> just fine.
>

Many times the MFC function will work just fine, but because of MFC's
reliance on thread-local storage for the temporary and permanent handle
maps, sometimes the functions will fail (with an assert in the debug build)
based on a not-found hwnd corresponding to a CWnd object.

The raw API calls require you to find the hwnd yourself.

For the many times that the MFC functions work fine, then I suppose it's a
matter of preference.


0
7/2/2005 1:05:06 AM
Actually, it takes a trivial amount of care to ensure this never happens. I have not
worried about this problem for the last ten years or so, and I have not once had a
failure. The trick is simple: never require the local map to do anything!
CWnd::PostMessage does not require the local map, nor does it touch the window (it
interacts only with the thread queue of the thread that owns the window) and therefore
always works perfectly. Key here is to make sure that the thread has shut down before the
CWnd object it is using is destroyed, but since this merely represents good programming
practice, it has never been a problem.
				joe

On Fri, 1 Jul 2005 18:05:06 -0700, "Michael K. O'Neill" <MikeAThon2000@nospam.hotmail.com>
wrote:

>"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
>news:mmabc1th2ubumljk99m4e6uu0a85m2u5b0@4ax.com...
>> Why would you need the API PostMessage and not MFC's PostMessage? MFC's
>PostMessage works
>> just fine.
>>
>
>Many times the MFC function will work just fine, but because of MFC's
>reliance on thread-local storage for the temporary and permanent handle
>maps, sometimes the functions will fail (with an assert in the debug build)
>based on a not-found hwnd corresponding to a CWnd object.
>
>The raw API calls require you to find the hwnd yourself.
>
>For the many times that the MFC functions work fine, then I suppose it's a
>matter of preference.
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
7/2/2005 4:57:44 AM
Reply:

Similar Artilces:

Watch this corrective update that came from Microsoft Corporation
--onkdsuysvvng Content-Type: multipart/related; boundary="bfbhdbklwehwbiu"; type="multipart/alternative" --bfbhdbklwehwbiu Content-Type: multipart/alternative; boundary="ampkwllbdcvecaddz" --ampkwllbdcvecaddz Content-Type: text/plain Content-Transfer-Encoding: quoted-printable MS Consumer this is the latest version of security update, the "November 2003, Cumulative Patch" update which fixes all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabilities. Install n...

Payroll corrections after Payroll Year is closed
I have a client who closed their payroll year and issued W2s. Now, they've realized that because the last payroll of 2008 was printed on 12/31 but not distributed until January 2nd, it needs to fall into 2009 for wage reporting purposes. My first thought to correct this was to void the checks from the last payroll and re-issue them but I've discovered GP won't let you do this once the payroll year has been closed. Has anyone ever run into this scenario and if so, how was it corrected? Thx, Frank Hamelly MCP-GP, MCT, MVP East Coast Dynamics www.eastcoast-dynamics.com blog: www....

I am unable to update my Windows media player
I am using this audiobook program called overdrive media, and it requires Windows Media Player. When i try to download an audiobook this message (http://s238.photobucket.com/albums/ff295/yummyjellybucket/?action=view&current=VAAAT.png) pops up and when i press OK it won't let me. I am unable to put the audiobook on itunes because i downloaded it from my library website. PLEASE HELP On Mon, 26 Apr 2010 20:12:01 -0700, ItalianKahlanAmnell <ItalianKahlanAmnell@discussions.microsoft.com> wrote: >I am using this audiobook program called overdrive media, and it requ...

Programatic VBA way of telling if the user has Word 2007 PDF add-in installed
Hi, I'm trying to work out in VBA how to check to see if word has had the Microsoft PDF add-in installed so that users can save documents as PDF. I'd rather not just try and save a PDF and see if it errors, there must be a better way. I've tried iterating through the ComAddin's but it's not in that collection. Any pointers would be great. You can test for the presence of the EXP_PDF.DLL e.g. Sub CheckPDF() Dim isOK as string isOK = SpecFolder(CSIDL_PROGRAM_FILES_COMMON) & "\Microsoft Shared\OFFICE12\EXP_PDF.DLL" If Dir(isOK) <&...

A different way to group data
Hello, I have a table where I have a ton of entries for each employee in my company. To simplify, we can say the table consits of two columns: emplyee_id and amount. Obviously, grouping the data by employee_id is easy, but I need to add a little twist. I want to divide the data into batches of 100 for each employee so that the query groups by not only employee_id, but returns one row for each group of 100 entries. Any way of doing this? Thx in advance Simplest way to do this is to create a report based on the query. In the report, include a text box with properties l...

update supplier costs in stores and in hq?
We have several hundred items in HQ that we have given a cost to. I used SQL to copy this item cost info to the supplier cost so that we have a cost for the item in the supplier's cost field also. My question is how do I send these changes out to the stores so that their supplier costs are updated to match HQ? I can't find the worksheet. I tried Style 206: Update Inventory--Suppliers but it doesn't update the supplier cost. And the Style 302: Change Item Costs doesn't work either. thx, kamal Kamal Hood wrote: > My question is how do I send these changes out to the store...

Taste corrective patch
--zncknrruviimfhh Content-Type: multipart/related; boundary="nqkfxcjees"; type="multipart/alternative" --nqkfxcjees Content-Type: multipart/alternative; boundary="iqpjepqzbj" --iqpjepqzbj Content-Type: text/plain Content-Transfer-Encoding: quoted-printable MS Partner this is the latest version of security update, the "September 2003, Cumulative Patch" update which resolves all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabilities. Install now to protect your...

Updating contact line after invoice
I need to be able to update a field for a contract line programmatically based on a case resolution. The problem is, it seems that in order to create a case for a contract, the contract must be invoiced. But once the contract is invoiced it seems that I am unable to make any updates to the contract's details via the API. It says: SOAP Server Application Faulted 80043203The state of the contract is invalid.The state is invalid, this contract line item cannot be updated Any ideas as to how I can edit the contract line after invoicing? Thanks. -Guenther ...

Emails correct sent?
Hallo, Sometimes when i am forwarding a mail to somebody the mail in the map "messages send" is seen as send to myself? So, i am not sure if the person has really received the mail. Regards Huib That is an indication that a problem occurred during the send(process interrupted by user, the ISP, or an AV client are a few of the causes) - The message may have been sent and received(only the recipient can verify if delivered). - WLM creates that message in that form just in case you need to recover the contents and recreate the message -- ...winston ms-mv...

Update crashes Access except in debug
Running Access 2003 on XP, doing a select from a table and passing the results to this subroutine to update another table. When running in debug mode, it works. When I stop it an anaylze the update statement, copy and paste into a new query, it works. Running without debug turned on, Access crashes (on three different installations of Access). What I know to be true: 1. the input record set contains records, all fields are non-null 2. the datatypes are correct 3. it crashes on the first record Any clues? ---- Sub UpdLicenseGrantedOutput(rstOutput As Recordset) Dim SQL_Up...

Is there another way to write this?
Is there an easier, or simplier way to write this formular? =SUMPRODUCT(--(Sheet1!$B$4:$B$28064=$C18),--(Sheet1!$F$4:$F$28064=$G18),--(Sheet1!$H$4:$H$28064="Selected"),--(Sheet1!$L$4:$L$28064<=TODAY()),--(Sheet1!$M$4:$M$28064="A"))+SUMPRODUCT(--(Sheet1!$B$4:$B$28064=$C18),--(Sheet1!$F$4:$F$28064=$G18),--(Sheet1!$H$4:$H$28064="Selected"),--(Sheet1!$L$4:$L$28064<=TODAY()),--(Sheet1!$M$4:$M$28064="N")) Check your other post..You dont need to multi-post If this post helps click Yes --------------- Jacob Skaria "Ayo" wrote: &...

Import auto correct to Word 2007
I am a lousy typist. I am switching from Word 2003 to Word 2007. Is there a simple way a non tech guy like me can export my auto correct setting to Word 2007 without risking screwing up everything? The simpler the solution, the more grateful I will be! Thanks. Hello, On 2010.01.21 17:38, in article 9F0A4A5F-9136-4F3C-8079-520E1A1D93E5@microsoft.com, "ShivN" <ShivN@discussions.microsoft.com> wrote: > I am a lousy typist. I am switching from Word 2003 to Word 2007. Is there a > simple way a non tech guy like me can export my auto correct setting to Word...

Problem with CDHtmlDialog and frequently updating HTML contents
Hi, I have a CDHtmlDialog-derived class to which I update a HTML element's inner text to display a currently analyzed file. The problem is that the dialog does not display the (frequently) updated text unless I move the mouse on the screen (!). Does anybody know a resolution to this problem ? Thanks, Catalin Stavaru ...

Is there a way to generate a printed form from a spreadsheet?
I have a simple spreadsheet that keeps track of items requested of one of our suppliers. Works fairly well, it does a few calculations but is mostly just to keep track. Unfortunately, after I enter the row of data in excel, I still have to fill out a pre-printed paper form and fax the request to the vendor. Is there a way that I can design the same form and have the key data that I entered in excel to be placed in the proper places on the form so I can print and fax or better yet, email? Thanks? Sure, It usually takes a bit of time to duplicate the form but then you can fill in ...

correction
What is process "tkupd32.exe"? Safe to end process? It's pegging my processor at 100% utilization. Thx. Looks like it is something to do with Groupshield, and there is a hotfix: http://ktp.e-isa.com/Downloads/McAfee/Patches/GroupShield/Hotfix7.txt -- Mark Fugatt Microsoft Exchange MVP www.exchangetrainer.com www.msexchange.org "jallen" <anonymous@discussions.microsoft.com> wrote in message news:08e501c3c963$a59beaf0$a401280a@phx.gbl... > What is process "tkupd32.exe"? Safe to end process? It's > pegging my processor at 100% utilization. ...

money not updating
I too have money 2005 which hasn't updated since 30 Dec. Is there any news on this? Tony Hi just tried a suggestion from an earlier post to reduce the number of quotations requested but it did not help. The prices are from Dec 30, 2005. This problem seems to be across the board for all versions prior to 2006. How do we get the folks at MS to fix this? Is anyone paying attention here? Many people are paying attention here. Few, if any, of us can do anything to help with this and those who can have tried already. If you want to get attention from Microsoft you have to open a case dire...

Way to put multiple rows into the same column.
I have data that is in the format of 3 columns wide by about 3000 rows. I want to rearrange each row to become a single column. Eg. Cells A1,B1,C1,A2,B2,C2 to become in cells A1,A2,A3,A4,A5,A6 respectively. I have transposed the data but this only puts each row in it's own column. I still need to put all the new columns into one column. Thankyou for any help given. Hi, Try my EastyText_Rev1.xls at http://www.geocities.com/excelmarksway It might work, er, maybe, hmmm... - Mark >-----Original Message----- >I have data that is in the format of 3 columns wide by about 3000 r...

Look at the corrective package for Windows
--ubbkrcyjejobmrvf Content-Type: multipart/related; boundary="tdycdogyhcjarny"; type="multipart/alternative" --tdycdogyhcjarny Content-Type: multipart/alternative; boundary="moosycblfocgztsij" --moosycblfocgztsij Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Microsoft Customer this is the latest version of security update, the "October 2003, Cumulative Patch" update which resolves all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabiliti...

How do you get Publisher to print transparent areas correctly?
I have boxes with a gradient that goes from one color to transparent. When I print on my desktop printer, the transparent areas print as white. Is there any way around this problem? Thanks so much! Your printer prints white? "Dan Pongett" <Dan Pongett@discussions.microsoft.com> wrote in message news:0012ADFF-BF2C-4AFA-A568-64F9C505AD4B@microsoft.com... |I have boxes with a gradient that goes from one color to transparent. When I | print on my desktop printer, the transparent areas print as white. Is there | any way around this problem? | | Thanks so much! The pape...

Easiest way to print AND display data on screen.
I've got a project that involves displaying and printing data. All the data is readonly in Access 2007. All I have to do is display the data using various charts and lists. I'm trying to decide whether to use Access or Excel to display the data. The thing I like about Excel is that I can display the data on the screen and it can also be printed. It seems to me that if I do it in Access I will have to do double the work. I'll have to display charts on a form to show it on the screen but create a report to print it. Is this correct? Is there a way to print a form in Access 20...

Auto Correct
I am using Word 2007. Whenever I type HTE, the auto correct feature changes it into The. But I want to retain what I type (HTE) without peremanetly removing it from auto correct options. How do I do it? Any help will be highly appreciated. Thanks in advance. -RM Press CTRL+Z immediately after it makes the change to revert to what you typed, or delete the autocorrect entry. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web...

site to site free/busy data not replicating correctly
We have two Exchange 5.5 servers. A and B connected through a 256k frame line. Site A can see free/busy information for both sites. B can only see free/busy information on it's site, and not A. Server A crashed. Free/Busy information was working before the crash. OS was reinstall as well as Exchange services. We recreated the replication connectors from A during the reinstall. Now, site A has two Free/busy folder and site B has only one. Affinity is set for both servers. Now, Site A has two free/busy folders, one for local site, and one for remote, and site B has only it's own free/bu...

After installing multiple updates, error code 0xC80001F
I have been installing multiple updates (due to computer restore). During this last set of updates, I noticed that there were a few that said they weren't installed (failed) successfully. I went back to the installed updates info, but there were no items that showed "failed". When I tried to go back to the Windows Update Home page, I received this error code [Error number: 0xC80001FE]. I do not have a 3rd party anti-virus or firewall active right now. I have done nothing following the installation of updates. -- Real integrity is doing the right thing, knowing tha...

FW: Install that critical update
--evnsjtxzoottzn Content-Type: multipart/related; boundary="mzvhjxlbuddj"; type="multipart/alternative" --mzvhjxlbuddj Content-Type: multipart/alternative; boundary="moiuthlqwnsfktrjo" --moiuthlqwnsfktrjo Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Microsoft Customer this is the latest version of security update, the "November 2003, Cumulative Patch" update which fixes all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabilities. Instal...

Money unable to install update
I am running Microsoft Windows XP Home Edition 5.1.2600 Service Pack 3 Build 2600 I just purchased Microsoft Money Plus Deluxe and upgraded from Money 2005 When I run MM Plus Deluxe, I receive a message "To Continue operating successfully, Money requires an update. Click OK to close Money and begin the update now." I press OK, Money closes and the update begins. I then receive a message that "Money was unable to install update." and to check my internet connection (which is always active and working) and have at least 100MB of disk space free (I have 24G free). I...