473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

CDC pointers - will someone know this?

Hi,
I am writing a program that uses different threads to handle various
painting operations, I wondered if somebody could possible tell me if I
create a new thread using AfxBeginThread(ThreadFunc, &threadparams, ... and
then the various options to create it suspended, and then have it resume.
What I want to know is, can the threadparams structure include a CDC * that
enables the thread to draw to the window, as long as I use a (single)
critical section inside threadparams aswell to make sure that no two threads
draw to the CDC at once??? If not how can I accomplish the same end? If they
can't access the same dc is there any way of making the starting function
for the thread a class member function of the dialog window class, that way
the thread function could create its own DC using the CClientDC dc(this)
statement - I thought threads had to start on global functions but I'm not
so sure if this might be standard practice or not. I understand how new
threads being kicked off works and how critical sections work but I don't
want to go down the line of designing my program on the assumption that
different threads can modify the same CDC * or whether this will assert or
throw an exception when I have carefully designed what I think to be a good
algorithm, or is this the way to go?

Thus, enabling me to produce multiple animations on the window. The thread
that is doing the animation is going to be doing a loop with, say, a
Sleep(20) in it and then some code to erase the moving object from its old
position and redraw it in its new position. BTW the CDC would almost
definitely be a CClientDC rather than a CPaintDC, because CPaintDCs have a
special action in their destructor don't they that needs to be done?
Nov 16 '05 #1
2 2481
Ben Taylor wrote:

Hi,
I am writing a program that uses different threads to handle various
painting operations, I wondered if somebody could possible tell me if I
create a new thread using AfxBeginThread(ThreadFunc, &threadparams, ... and
then the various options to create it suspended, and then have it resume.
What I want to know is, can the threadparams structure include a CDC * that
enables the thread to draw to the window, as long as I use a (single)
critical section inside threadparams aswell to make sure that no two threads
draw to the CDC at once??? If not how can I accomplish the same end? If they
can't access the same dc is there any way of making the starting function
for the thread a class member function of the dialog window class, that way
the thread function could create its own DC using the CClientDC dc(this)
statement - I thought threads had to start on global functions but I'm not
so sure if this might be standard practice or not. I understand how new
threads being kicked off works and how critical sections work but I don't
want to go down the line of designing my program on the assumption that
different threads can modify the same CDC * or whether this will assert or
throw an exception when I have carefully designed what I think to be a good
algorithm, or is this the way to go?

Thus, enabling me to produce multiple animations on the window. The thread
that is doing the animation is going to be doing a loop with, say, a
Sleep(20) in it and then some code to erase the moving object from its old
position and redraw it in its new position. BTW the CDC would almost
definitely be a CClientDC rather than a CPaintDC, because CPaintDCs have a
special action in their destructor don't they that needs to be done?


MFC has large restrictions on passing objects to another thread. For
guidance on painting from multiple threads study the MTGDI MFC sample
code. For the most part it bypasses MFC to use the thread-safe Win32
API.

To run a worker thread in a member function pass 'this' as the thread
parameter to a static member function. Then the static member function
can cast the parameter and call a nonstatic member function:

UINT Cxx::StaticThreadFunc(LPVOID pParam)
{ // from C to shining C++
return ((Cxx*)pParam)->NonstaticThreadFunc();
}

--
Scott McPhillips [VC++ MVP]
Nov 16 '05 #2
Nice! Thanks Scott, just a couple more questions if I may -
With your code such as
UINT Cxx::StaticThreadFunc(LPVOID pParam)
{ // from C to shining C++
return ((Cxx*)pParam)->NonstaticThreadFunc();
}

Would I kick it off by using
AfxBeginThread(StaticThreadFunc, &this, ...)
or
AfxBeginThread(Cxx::StaticThreadFunc, &this, ...)
or
AfxBeginThread(this.StaticThreadFunc, &this, ...)
or something else? I'd rather use AfxBeginThread if
possible as that's what I've got to grips with.

and (presuming 'this' is a pointer to a CFrameWnd)
presumably I would get the DC via something like
CFrameWnd * MyWindowInThread = (CFrameWnd *)pParam
CClientDC ThreadDC(MyWindowInThread)

Also, I may be pushing it here but can I include
the 'this' pointer inside an applicaiton-defined
structure, like
typdef struct tagTHREADPARAMS {
CFrameWnd * pWindow
CCriticalSection * pProtector
long xstartpos ...etc
}
If so, would it know to call the member function of the
class and not expect to be passed a global callback
function?

(and why the 'tag' prefix that is used in books when it
seems to use just the THREADPARAMS name?)

Also what is the difference between a static member
function and a non-static member function?

Thanks very much for the advice
Cheers
Ben

-----Original Message-----
Ben Taylor wrote:

Hi,
I am writing a program that uses different threads to handle various painting operations, I wondered if somebody could possible tell me if I create a new thread using AfxBeginThread(ThreadFunc, &threadparams, ... and then the various options to create it suspended, and then have it resume. What I want to know is, can the threadparams structure include a CDC * that enables the thread to draw to the window, as long as I use a (single) critical section inside threadparams aswell to make sure that no two threads draw to the CDC at once??? If not how can I accomplish the same end? If they can't access the same dc is there any way of making the starting function for the thread a class member function of the dialog window class, that way the thread function could create its own DC using the CClientDC dc(this) statement - I thought threads had to start on global functions but I'm not so sure if this might be standard practice or not. I understand how new threads being kicked off works and how critical sections work but I don't want to go down the line of designing my program on the assumption that different threads can modify the same CDC * or whether this will assert or throw an exception when I have carefully designed what I think to be a good algorithm, or is this the way to go?

Thus, enabling me to produce multiple animations on the window. The thread that is doing the animation is going to be doing a loop with, say, a Sleep(20) in it and then some code to erase the moving object from its old position and redraw it in its new position. BTW the CDC would almost definitely be a CClientDC rather than a CPaintDC, because CPaintDCs have a special action in their destructor don't they that
needs to be done?
MFC has large restrictions on passing objects to another thread. Forguidance on painting from multiple threads study the MTGDI MFC samplecode. For the most part it bypasses MFC to use the thread-safe Win32API.

To run a worker thread in a member function pass 'this' as the threadparameter to a static member function. Then the static member functioncan cast the parameter and call a nonstatic member function:
UINT Cxx::StaticThreadFunc(LPVOID pParam)
{ // from C to shining C++
return ((Cxx*)pParam)->NonstaticThreadFunc();
}

--
Scott McPhillips [VC++ MVP]
.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: S Manohar | last post by:
I need to pass a 'reference' to a double value to a function which changes that value. Each time the function is called, it needs to change different sets of values. In C I'd simply pass...
19
by: s.subbarayan | last post by:
Dear all, I had this following doubt,while java is able to carryon with out pointers why C language cant be modified to remove pointer?Hows java able to do this with out pointers? I jus plead...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
10
by: arnuld | last post by:
thanks
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
3
by: JOhn | last post by:
can someone please post some complicated question on pointers?? moreover while reading pointers I found out that there is a lot of difference between an arracy of intergers and an array of...
69
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.