Accessing a Progress bar in View class from thread in mainframe class

oliverbowen used Ask the Experts™
I have a Progress Bar control inside my view class for an MFC SDI application.

I created a thread and set the message handlers and thread functions in the MainFrame class.

When I attempt to compile I get an error saying my progress bar variable/control cannot be found where I try to update it in the thread function defined/implemented in the mainframe class.

How do I get access to that control?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Can you post the error message from the compiler?
Without it I can only guess that you forgot to add an appropriate #include in the cpp-file where you use the bar.
You shouldn.t directly update the progress controlo from the thread function. The best way is to post a private message from the thread to the view and, from the view's message handler, update the control.

You can give a look at this sample (it refers to a dialog, but you can apply it to a view in an easy way):
First pass a pointer of the view to the thread  (should be this if you create the thread in view).

In the thread function you can cast the void* argument back to view class pointer. That way you have access to member functions of the view or member data.

Add a int member to view for the progress 0 ... 100. Set the counter in your thread whenever you want.

Add OnTimer handler function to view and whenever it was called update the progress bar using the member written from thread. As it is read-only all is safe.


Ok I got it figured out.

I moved the message handlers and custom messages into the view class, which is where the progress bar control is.

Now in my thread as I'm doing my processing I post a message to the update message handler and it works great.

I pass the counter to the message handler through the lparam of the sendmessage function and that way I'm able to increment the cprogressbar.

Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial