Solved

Multi Threads (Visual C++, MFC)

Posted on 1998-09-04
7
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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