Solved

Multi Threads (Visual C++, MFC)

Posted on 1998-09-04
7
340 Views
Last Modified: 2013-12-03
I built an MDI application with Visual C++ 5 (automatically with "MFC appWizard - EXE").
The View Class draws itself to the child window through the CDC pDC variable in View::OnDraw, to the device context in the usual way.
I'd like to do the drawing in a separate thread, to create a new 'drawing' thread. (In my application the drawing is a long task). This is a very common thing to do.

I encountered severe problems doing that. Many critical errors were created.
note: I work with WinNT.

Please answer as clear as possible and give an example, because I had many problems with this issue.
0
Comment
Question by:giora
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1414209
The problem is you need to protect access to objects which are used in multiple threads.  This includes MFC objects.  Otherwise you'll get synchronization problems which will produce unpredictable results.
0
 

Author Comment

by:giora
ID: 1414210
Please give more details about protecting access, I don't understand.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1414211
I would suggest that you allocate a memory DC and a memory Bitmap into that DC, which
has the same size like the painting area. Then, let your threads paint into this DC,
and - perhaps using a timer - send every 500 msec or so an Invalidate() command to the
main window, which blits the memory DC into the visible DC. This is also more common
because if the window gets overlapped, the repainting does not start from the beginning.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Expert Comment

by:Murali Devarakonda
ID: 1414212
I can give you code for a thread class that takes a handle to a static control and continuously draws the current time on it.

The thread is give handles to it's parent and the static control via the constructor.

You can extrapolate this code to your situation without much difficulty.

Murali Krishna Devarakonda
0
 
LVL 1

Expert Comment

by:Murali Devarakonda
ID: 1414213
I can give you code for a thread class that takes a handle to a static control and continuously draws the current time on it.

The thread is give handles to it's parent and the static control via the constructor.

You can extrapolate this code to your situation without much difficulty.

Murali Krishna Devarakonda
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1414214
MFC Objects were not intended to be used by more then one thread. VC's reference states, that in case of need of sharing CWnd or CDC class, you should pass only the handle (m_hDC), and it should create the CDC object using CDC::FromHandle
0
 
LVL 3

Accepted Solution

by:
plaroche earned 100 total points
ID: 1414215
Create a memory DC and a compatible bitmap as members of your view class.  You will also need a critical section so that your view and thread do not access that memory dc at the same time.

In your view's OnDraw or OnPaint check the critical section(under NT you can use TryEnterCriticalSection) if you cannot enter it then it's because your thread has it for itself. Then you can draw a "Please wait" message.  When your thread has finished drawing have it release the critical section and invalidate your view.

The invalidate will cause another redraw and the your view just blits the bitmap because it has entered the critical section.

This is a solution I have implemented using multiple views and windows in these views. It works quite well even with 100 windows each having its drawing thread.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question