Solved

Multi Threads (Visual C++, MFC)

Posted on 1998-09-04
7
330 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
Comment Utility
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
Comment Utility
Please give more details about protecting access, I don't understand.
0
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

15 Experts available now in Live!

Get 1:1 Help Now