?
Solved

Multi Threads (Visual C++, MFC)

Posted on 1998-09-04
7
Medium Priority
?
353 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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 300 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

765 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