Solved

Multi Threads (Visual C++, MFC)

Posted on 1998-09-04
7
332 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:MuraliKrishna
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:MuraliKrishna
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now