MFC and User Defined objects

Greetings,

I am trying to PostMessage from a user defined generic class/object in
MFC Dialog based application to Dialog/main window.  I don't know how
to do that.  I am utilizing worker threads.  A thread instantiates an
object(from user defined generic class) and utilizes newly created
object. What user defined object code does is actually launch a
process, for sake for argument lets say, Windows Notepad.exe using
CreateProcess.   As long as that process is running, I want to
PostMessage that updates a progress bar on Main GUI dialog.  I can do
it if I embed the code right into my application by writing the code
directly into it or declare, define and use member function of Dialog/
main program itself.  But I want to learn new material by incorporting
a user defined class and then interacting with main application code.
To recap:

MFC Dialog based Main application exectues->  It launches a thread. ->
Thread uses user defined generic class to instantiate a new object.  -
> New Object  should run and  should PostMessage to Main window to
update progress bar.

Again, I don't know how to do this.  How to tell newly created object
from user defined class, which is running within a Worker Thread, to
successfully post message to main GUI?  I know that Worker threads are
not allowed to interact with GUI objects.

Any good software engineering solution will be greatly appreciated.  I
thank you.

0
3/13/2007 5:55:36 PM
vc.mfc 33608 articles. 0 followers. Follow

32 Replies
1072 Views

Similar Articles

[PageSpeed] 38

I would add a member CWnd *p_UIWnd; to the object and assign it to a UI 
window when the object is created.  Then you could do something like:

p_UIWnd->PostMessage() when you want to update the window.  You will have to 
create your own message based off of WM_APP to do this and put in a handler.

in .h file:

 LRESULT OnServerStateChange(WPARAM wParam, LPARAM lParam);
#define WM_UPDATE_SERVER_STATE  (WM_APP + 1)

in .cpp file message handler section:

ON_MESSAGE(WM_UPDATE_SERVER_STATE, OnServerStateChange)

LRESULT CMainFrame::OnServerStateChange(WPARAM wParam, LPARAM lParam)
{
    // Do something with information
     return 0L;
}

From thread code:

p_UIWnd->PostMessage(WM_UPDATE_SERVER_STATE,0,0L);

HTH,

Tom

"one-trick-pony" <worldofpain.aamir@gmail.com> wrote in message 
news:1173808536.856153.253810@q40g2000cwq.googlegroups.com...
> Greetings,
>
> I am trying to PostMessage from a user defined generic class/object in
> MFC Dialog based application to Dialog/main window.  I don't know how
> to do that.  I am utilizing worker threads.  A thread instantiates an
> object(from user defined generic class) and utilizes newly created
> object. What user defined object code does is actually launch a
> process, for sake for argument lets say, Windows Notepad.exe using
> CreateProcess.   As long as that process is running, I want to
> PostMessage that updates a progress bar on Main GUI dialog.  I can do
> it if I embed the code right into my application by writing the code
> directly into it or declare, define and use member function of Dialog/
> main program itself.  But I want to learn new material by incorporting
> a user defined class and then interacting with main application code.
> To recap:
>
> MFC Dialog based Main application exectues->  It launches a thread. ->
> Thread uses user defined generic class to instantiate a new object.  -
>> New Object  should run and  should PostMessage to Main window to
> update progress bar.
>
> Again, I don't know how to do this.  How to tell newly created object
> from user defined class, which is running within a Worker Thread, to
> successfully post message to main GUI?  I know that Worker threads are
> not allowed to interact with GUI objects.
>
> Any good software engineering solution will be greatly appreciated.  I
> thank you.
> 

0
tom.nospam (3240)
3/13/2007 6:41:34 PM
one-trick-pony wrote:
> Greetings,
> 
> I am trying to PostMessage from a user defined generic class/object in
> MFC Dialog based application to Dialog/main window.  I don't know how
> to do that.  I am utilizing worker threads.  A thread instantiates an
> object(from user defined generic class) and utilizes newly created
> object. What user defined object code does is actually launch a
> process, for sake for argument lets say, Windows Notepad.exe using
> CreateProcess.   As long as that process is running, I want to
> PostMessage that updates a progress bar on Main GUI dialog.  I can do
> it if I embed the code right into my application by writing the code
> directly into it or declare, define and use member function of Dialog/
> main program itself.  But I want to learn new material by incorporting
> a user defined class and then interacting with main application code.
> To recap:
> 
> MFC Dialog based Main application exectues->  It launches a thread. ->
> Thread uses user defined generic class to instantiate a new object.  -
> 
>>New Object  should run and  should PostMessage to Main window to
> 
> update progress bar.
> 
> Again, I don't know how to do this.  How to tell newly created object
> from user defined class, which is running within a Worker Thread, to
> successfully post message to main GUI?  I know that Worker threads are
> not allowed to interact with GUI objects.
> 
> Any good software engineering solution will be greatly appreciated.  I
> thank you.
> 

Your object in a worker thread needs to be passed a CWnd* or HWND for 
the main window.  This can be passed into to the thread function via the 
thread parameter.

Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
object.  Example here...
http://vcfaq.mvps.org/mfc/12.htm

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/13/2007 6:49:53 PM
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
> one-trick-pony wrote:
>> Greetings,
>>
>> I am trying to PostMessage from a user defined generic class/object in
>> MFC Dialog based application to Dialog/main window.  I don't know how
>> to do that.  I am utilizing worker threads.  A thread instantiates an
>> object(from user defined generic class) and utilizes newly created
>> object. What user defined object code does is actually launch a
>> process, for sake for argument lets say, Windows Notepad.exe using
>> CreateProcess.   As long as that process is running, I want to
>> PostMessage that updates a progress bar on Main GUI dialog.  I can do
>> it if I embed the code right into my application by writing the code
>> directly into it or declare, define and use member function of Dialog/
>> main program itself.  But I want to learn new material by incorporting
>> a user defined class and then interacting with main application code.
>> To recap:
>>
>> MFC Dialog based Main application exectues->  It launches a thread. ->
>> Thread uses user defined generic class to instantiate a new object.  -
>>
>>>New Object  should run and  should PostMessage to Main window to
>>
>> update progress bar.
>>
>> Again, I don't know how to do this.  How to tell newly created object
>> from user defined class, which is running within a Worker Thread, to
>> successfully post message to main GUI?  I know that Worker threads are
>> not allowed to interact with GUI objects.
>>
>> Any good software engineering solution will be greatly appreciated.  I
>> thank you.
>>
>
> Your object in a worker thread needs to be passed a CWnd* or HWND for the 
> main window.  This can be passed into to the thread function via the 
> thread parameter.
>
> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
> object.  Example here...
> http://vcfaq.mvps.org/mfc/12.htm
>
> -- 
> Scott McPhillips [VC++ MVP]
>

I would pass the HWND instead of the CWnd, you never know CWnd my not be 
thread safe. Or the CWnd object might get destroyed without the thread 
knowing about it.

AliR.


0
AliR3470 (3236)
3/13/2007 7:34:31 PM

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
> news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
>> one-trick-pony wrote:
>>> Greetings,
>>>
>>> I am trying to PostMessage from a user defined generic class/object in
>>> MFC Dialog based application to Dialog/main window.  I don't know how
>>> to do that.  I am utilizing worker threads.  A thread instantiates an
>>> object(from user defined generic class) and utilizes newly created
>>> object. What user defined object code does is actually launch a
>>> process, for sake for argument lets say, Windows Notepad.exe using
>>> CreateProcess.   As long as that process is running, I want to
>>> PostMessage that updates a progress bar on Main GUI dialog.  I can do
>>> it if I embed the code right into my application by writing the code
>>> directly into it or declare, define and use member function of Dialog/
>>> main program itself.  But I want to learn new material by incorporting
>>> a user defined class and then interacting with main application code.
>>> To recap:
>>>
>>> MFC Dialog based Main application exectues->  It launches a thread. ->
>>> Thread uses user defined generic class to instantiate a new object.  -
>>>
>>>>New Object  should run and  should PostMessage to Main window to
>>>
>>> update progress bar.
>>>
>>> Again, I don't know how to do this.  How to tell newly created object
>>> from user defined class, which is running within a Worker Thread, to
>>> successfully post message to main GUI?  I know that Worker threads are
>>> not allowed to interact with GUI objects.
>>>
>>> Any good software engineering solution will be greatly appreciated.  I
>>> thank you.
>>>
>>
>> Your object in a worker thread needs to be passed a CWnd* or HWND for the 
>> main window.  This can be passed into to the thread function via the 
>> thread parameter.
>>
>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
>> object.  Example here...
>> http://vcfaq.mvps.org/mfc/12.htm
>>
>> -- 
>> Scott McPhillips [VC++ MVP]
>>
>
> I would pass the HWND instead of the CWnd, you never know CWnd my not be 
> thread safe. Or the CWnd object might get destroyed without the thread 
> knowing about it.

There is no problem passing a CWnd* to a thread and using it to post 
messages.

If it is destroyed, it is a programming bug.
 

0
anyone3666 (139)
3/13/2007 8:41:35 PM
"Ian Semmel" <anyone@rocketcomp.com.au> wrote in message 
news:ee%23H9$aZHHA.4000@TK2MSFTNGP02.phx.gbl...
>
>
> "AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
> news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
>> news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
>>> one-trick-pony wrote:
>>>> Greetings,
>>>>
>>>> I am trying to PostMessage from a user defined generic class/object in
>>>> MFC Dialog based application to Dialog/main window.  I don't know how
>>>> to do that.  I am utilizing worker threads.  A thread instantiates an
>>>> object(from user defined generic class) and utilizes newly created
>>>> object. What user defined object code does is actually launch a
>>>> process, for sake for argument lets say, Windows Notepad.exe using
>>>> CreateProcess.   As long as that process is running, I want to
>>>> PostMessage that updates a progress bar on Main GUI dialog.  I can do
>>>> it if I embed the code right into my application by writing the code
>>>> directly into it or declare, define and use member function of Dialog/
>>>> main program itself.  But I want to learn new material by incorporting
>>>> a user defined class and then interacting with main application code.
>>>> To recap:
>>>>
>>>> MFC Dialog based Main application exectues->  It launches a thread. ->
>>>> Thread uses user defined generic class to instantiate a new object.  -
>>>>
>>>>>New Object  should run and  should PostMessage to Main window to
>>>>
>>>> update progress bar.
>>>>
>>>> Again, I don't know how to do this.  How to tell newly created object
>>>> from user defined class, which is running within a Worker Thread, to
>>>> successfully post message to main GUI?  I know that Worker threads are
>>>> not allowed to interact with GUI objects.
>>>>
>>>> Any good software engineering solution will be greatly appreciated.  I
>>>> thank you.
>>>>
>>>
>>> Your object in a worker thread needs to be passed a CWnd* or HWND for 
>>> the main window.  This can be passed into to the thread function via the 
>>> thread parameter.
>>>
>>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
>>> object.  Example here...
>>> http://vcfaq.mvps.org/mfc/12.htm
>>>
>>> -- 
>>> Scott McPhillips [VC++ MVP]
>>>
>>
>> I would pass the HWND instead of the CWnd, you never know CWnd my not be 
>> thread safe. Or the CWnd object might get destroyed without the thread 
>> knowing about it.
>
> There is no problem passing a CWnd* to a thread and using it to post 
> messages.
>
> If it is destroyed, it is a programming bug.
>
>

Why would that be a programming bug?   It's a window, it has a mind of its 
own.  And it's completely independent of the thread.  The thread my use the 
window to display some info, or what not, but the thread doesn't need a 
window to exists or do its work.

Lets say that the program is monitoring id on disk in a worker thread, and 
at the same time a dialog is displaying some information about the thread 
process. Lets say the user says I don't want to see this window anymore. So 
the window goes away (the programmer decides to destroy the window, since 
there is no way for the user to recall the dialog again, it doesn't 
necessarily mean that the thread would need to stop working also. Does it?

This might not be user friendly but its by no means forbidden or illegal for 
someone to do.

AliR.


0
AliR3470 (3236)
3/13/2007 8:54:07 PM
On Mar 13, 4:54 pm, "AliR \(VC++ MVP\)" <A...@online.nospam> wrote:
> "Ian Semmel" <any...@rocketcomp.com.au> wrote in message
>
> news:ee%23H9$aZHHA.4000@TK2MSFTNGP02.phx.gbl...
>
>
>
>
>
> > "AliR (VC++ MVP)" <A...@online.nospam> wrote in message
> >news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
> >> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
> >>news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
> >>> one-trick-pony wrote:
> >>>> Greetings,
>
> >>>> I am trying to PostMessage from a user defined generic class/object in
> >>>> MFC Dialog based application to Dialog/main window.  I don't know how
> >>>> to do that.  I am utilizing worker threads.  A thread instantiates an
> >>>> object(from user defined generic class) and utilizes newly created
> >>>> object. What user defined object code does is actually launch a
> >>>> process, for sake for argument lets say, Windows Notepad.exe using
> >>>> CreateProcess.   As long as that process is running, I want to
> >>>> PostMessage that updates a progress bar on Main GUI dialog.  I can do
> >>>> it if I embed the code right into my application by writing the code
> >>>> directly into it or declare, define and use member function of Dialog/
> >>>> main program itself.  But I want to learn new material by incorporting
> >>>> a user defined class and then interacting with main application code.
> >>>> To recap:
>
> >>>> MFC Dialog based Main application exectues->  It launches a thread. ->
> >>>> Thread uses user defined generic class to instantiate a new object.  -
>
> >>>>>New Object  should run and  should PostMessage to Main window to
>
> >>>> update progress bar.
>
> >>>> Again, I don't know how to do this.  How to tell newly created object
> >>>> from user defined class, which is running within a Worker Thread, to
> >>>> successfully post message to main GUI?  I know that Worker threads are
> >>>> not allowed to interact with GUI objects.
>
> >>>> Any good software engineering solution will be greatly appreciated.  I
> >>>> thank you.
>
> >>> Your object in a worker thread needs to be passed a CWnd* or HWND for
> >>> the main window.  This can be passed into to the thread function via the
> >>> thread parameter.
>
> >>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your
> >>> object.  Example here...
> >>>http://vcfaq.mvps.org/mfc/12.htm
>
> >>> --
> >>> Scott McPhillips [VC++ MVP]
>
> >> I would pass the HWND instead of the CWnd, you never know CWnd my not be
> >> thread safe. Or the CWnd object might get destroyed without the thread
> >> knowing about it.
>
> > There is no problem passing a CWnd* to a thread and using it to post
> > messages.
>
> > If it is destroyed, it is a programming bug.
>
> Why would that be a programming bug?   It's a window, it has a mind of its
> own.  And it's completely independent of the thread.  The thread my use the
> window to display some info, or what not, but the thread doesn't need a
> window to exists or do its work.
>
> Lets say that the program is monitoring id on disk in a worker thread, and
> at the same time a dialog is displaying some information about the thread
> process. Lets say the user says I don't want to see this window anymore. So
> the window goes away (the programmer decides to destroy the window, since
> there is no way for the user to recall the dialog again, it doesn't
> necessarily mean that the thread would need to stop working also. Does it?
>
> This might not be user friendly but its by no means forbidden or illegal for
> someone to do.
>
> AliR.



In that case, how will a HWnd help(instead of CWnd*)?

---
Ajay

0
ajaykalra (6842)
3/13/2007 9:02:31 PM
"Ajay Kalra" <ajaykalra@yahoo.com> wrote in message 
news:1173819751.344463.76230@b75g2000hsg.googlegroups.com...
> On Mar 13, 4:54 pm, "AliR \(VC++ MVP\)" <A...@online.nospam> wrote:
>> "Ian Semmel" <any...@rocketcomp.com.au> wrote in message
>>
>> news:ee%23H9$aZHHA.4000@TK2MSFTNGP02.phx.gbl...
>>
>>
>>
>>
>>
>> > "AliR (VC++ MVP)" <A...@online.nospam> wrote in message
>> >news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>> >> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
>> >>news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
>> >>> one-trick-pony wrote:
>> >>>> Greetings,
>>
>> >>>> I am trying to PostMessage from a user defined generic class/object 
>> >>>> in
>> >>>> MFC Dialog based application to Dialog/main window.  I don't know 
>> >>>> how
>> >>>> to do that.  I am utilizing worker threads.  A thread instantiates 
>> >>>> an
>> >>>> object(from user defined generic class) and utilizes newly created
>> >>>> object. What user defined object code does is actually launch a
>> >>>> process, for sake for argument lets say, Windows Notepad.exe using
>> >>>> CreateProcess.   As long as that process is running, I want to
>> >>>> PostMessage that updates a progress bar on Main GUI dialog.  I can 
>> >>>> do
>> >>>> it if I embed the code right into my application by writing the code
>> >>>> directly into it or declare, define and use member function of 
>> >>>> Dialog/
>> >>>> main program itself.  But I want to learn new material by 
>> >>>> incorporting
>> >>>> a user defined class and then interacting with main application 
>> >>>> code.
>> >>>> To recap:
>>
>> >>>> MFC Dialog based Main application exectues->  It launches a 
>> >>>> thread. ->
>> >>>> Thread uses user defined generic class to instantiate a new 
>> >>>> bject.  -
>>
>> >>>>>New Object  should run and  should PostMessage to Main window to
>>
>> >>>> update progress bar.
>>
>> >>>> Again, I don't know how to do this.  How to tell newly created 
>> >>>> object
>> >>>> from user defined class, which is running within a Worker Thread, to
>> >>>> successfully post message to main GUI?  I know that Worker threads 
>> >>>> are
>> >>>> not allowed to interact with GUI objects.
>>
>> >>>> Any good software engineering solution will be greatly appreciated. 
>> >>>> I
>> >>>> thank you.
>>
>> >>> Your object in a worker thread needs to be passed a CWnd* or HWND for
>> >>> the main window.  This can be passed into to the thread function via 
>> >>> the
>> >>> thread parameter.
>>
>> >>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from 
>> >>> your
>> >>> object.  Example here...
>> >>>http://vcfaq.mvps.org/mfc/12.htm
>>
>> >>> --
>> >>> Scott McPhillips [VC++ MVP]
>>
>> >> I would pass the HWND instead of the CWnd, you never know CWnd my not 
>> >> be
>> >> thread safe. Or the CWnd object might get destroyed without the thread
>> >> knowing about it.
>>
>> > There is no problem passing a CWnd* to a thread and using it to post
>> > messages.
>>
>> > If it is destroyed, it is a programming bug.
>>
>> Why would that be a programming bug?   It's a window, it has a mind of 
>> its
>> own.  And it's completely independent of the thread.  The thread my use 
>> the
>> window to display some info, or what not, but the thread doesn't need a
>> window to exists or do its work.
>>
>> Lets say that the program is monitoring id on disk in a worker thread, 
>> and
>> at the same time a dialog is displaying some information about the thread
>> process. Lets say the user says I don't want to see this window anymore. 
>> So
>> the window goes away (the programmer decides to destroy the window, since
>> there is no way for the user to recall the dialog again, it doesn't
>> necessarily mean that the thread would need to stop working also. Does 
>> it?
>>
>> This might not be user friendly but its by no means forbidden or illegal 
>> for
>> someone to do.
>>
>> AliR.
>
>
>
> In that case, how will a HWnd help(instead of CWnd*)?
>
> ---
> Ajay
>

PostMessage(hWnd,...);  will never crash. even if the window is long gone.

pWnd->PostMessage(....) will crash if pWnd has been deleted.

AliR.


0
AliR3470 (3236)
3/13/2007 9:09:49 PM
"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:xqEJh.3031$Qw.1448@newssvr29.news.prodigy.net...
>
> "Ajay Kalra" <ajaykalra@yahoo.com> wrote in message 
> news:1173819751.344463.76230@b75g2000hsg.googlegroups.com...
>> On Mar 13, 4:54 pm, "AliR \(VC++ MVP\)" <A...@online.nospam> wrote:
>>> "Ian Semmel" <any...@rocketcomp.com.au> wrote in message
>>>
>>> news:ee%23H9$aZHHA.4000@TK2MSFTNGP02.phx.gbl...
>>>
>>>
>>>
>>>
>>>
>>> > "AliR (VC++ MVP)" <A...@online.nospam> wrote in message
>>> >news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>>> >> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
>>> >>news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
>>> >>> one-trick-pony wrote:
>>> >>>> Greetings,
>>>
>>> >>>> I am trying to PostMessage from a user defined generic class/object 
>>> >>>> in
>>> >>>> MFC Dialog based application to Dialog/main window.  I don't know 
>>> >>>> how
>>> >>>> to do that.  I am utilizing worker threads.  A thread instantiates 
>>> >>>> an
>>> >>>> object(from user defined generic class) and utilizes newly created
>>> >>>> object. What user defined object code does is actually launch a
>>> >>>> process, for sake for argument lets say, Windows Notepad.exe using
>>> >>>> CreateProcess.   As long as that process is running, I want to
>>> >>>> PostMessage that updates a progress bar on Main GUI dialog.  I can 
>>> >>>> do
>>> >>>> it if I embed the code right into my application by writing the 
>>> >>>> code
>>> >>>> directly into it or declare, define and use member function of 
>>> >>>> Dialog/
>>> >>>> main program itself.  But I want to learn new material by 
>>> >>>> incorporting
>>> >>>> a user defined class and then interacting with main application 
>>> >>>> code.
>>> >>>> To recap:
>>>
>>> >>>> MFC Dialog based Main application exectues->  It launches a 
>>> >>>> thread. ->
>>> >>>> Thread uses user defined generic class to instantiate a new 
>>> >>>> ject.  -
>>>
>>> >>>>>New Object  should run and  should PostMessage to Main window to
>>>
>>> >>>> update progress bar.
>>>
>>> >>>> Again, I don't know how to do this.  How to tell newly created 
>>> >>>> object
>>> >>>> from user defined class, which is running within a Worker Thread, 
>>> >>>> to
>>> >>>> successfully post message to main GUI?  I know that Worker threads 
>>> >>>> are
>>> >>>> not allowed to interact with GUI objects.
>>>
>>> >>>> Any good software engineering solution will be greatly appreciated. 
>>> >>>> I
>>> >>>> thank you.
>>>
>>> >>> Your object in a worker thread needs to be passed a CWnd* or HWND 
>>> >>> for
>>> >>> the main window.  This can be passed into to the thread function via 
>>> >>> the
>>> >>> thread parameter.
>>>
>>> >>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from 
>>> >>> your
>>> >>> object.  Example here...
>>> >>>http://vcfaq.mvps.org/mfc/12.htm
>>>
>>> >>> --
>>> >>> Scott McPhillips [VC++ MVP]
>>>
>>> >> I would pass the HWND instead of the CWnd, you never know CWnd my not 
>>> >> be
>>> >> thread safe. Or the CWnd object might get destroyed without the 
>>> >> thread
>>> >> knowing about it.
>>>
>>> > There is no problem passing a CWnd* to a thread and using it to post
>>> > messages.
>>>
>>> > If it is destroyed, it is a programming bug.
>>>
>>> Why would that be a programming bug?   It's a window, it has a mind of 
>>> its
>>> own.  And it's completely independent of the thread.  The thread my use 
>>> the
>>> window to display some info, or what not, but the thread doesn't need a
>>> window to exists or do its work.
>>>
>>> Lets say that the program is monitoring id on disk in a worker thread, 
>>> and
>>> at the same time a dialog is displaying some information about the 
>>> thread
>>> process. Lets say the user says I don't want to see this window anymore. 
>>> So
>>> the window goes away (the programmer decides to destroy the window, 
>>> since
>>> there is no way for the user to recall the dialog again, it doesn't
>>> necessarily mean that the thread would need to stop working also. Does 
>>> it?
>>>
>>> This might not be user friendly but its by no means forbidden or illegal 
>>> for
>>> someone to do.
>>>
>>> AliR.
>>
>>
>>
>> In that case, how will a HWnd help(instead of CWnd*)?
>>
>> ---
>> Ajay
>>
>
> PostMessage(hWnd,...);  will never crash. even if the window is long gone.
>
> pWnd->PostMessage(....) will crash if pWnd has been deleted.
>
> AliR.
>
>

I am saying this assuming, worker thread will not do anything with the CWnd 
pointer other than post messages to it. Since manipulating the other 
thread's object directly is a no no. And since it's a no no, CWnd is not 
needed and it's handel is alot safer.

AliR.


0
AliR3470 (3236)
3/13/2007 9:18:25 PM
"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
> Your object in a worker thread needs to be passed a CWnd* or HWND for the 
> main window.  This can be passed into to the thread function via the 
> thread parameter.
>
> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
> object.  Example here...
> http://vcfaq.mvps.org/mfc/12.htm
>

Yes, except since it's the main dialog/window that is always being posted 
to, the worker thread can just call AfxGetMainWnd() instead of being passed 
it.

-- David

0
dc2983 (3206)
3/13/2007 9:58:21 PM
Hi Ali,

That's a good point.  I've never had trouble setting a CWnd pointer in 
thread code.  I typically call the function in the object before the thread 
is started with something like:

m_pMyThread.SetStatusWnd(CWnd *)

but your point seems logical.  Maybe I've just been lucky in using it a way 
it works.  One thing that is worth noting is that if the window that the 
CWnd * points to goes away (is closed) a call like 
m_StatusWnd->SendMessage() will cause a failure whereas the call to 
SendMessage() with an handle will simple be ignored.

Tom

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>
> I would pass the HWND instead of the CWnd, you never know CWnd my not be 
> thread safe. Or the CWnd object might get destroyed without the thread 
> knowing about it.
>
> AliR.
>
> 

0
tom.nospam (3240)
3/13/2007 10:34:26 PM
Ian,

You're right of course, but it depends on the window.  For example, if a 
pointer to a view is passed in the view may come and go and the program will 
have to know (the thread) whether or not the window/object is still valid.

Tom

"Ian Semmel" <anyone@rocketcomp.com.au> wrote in message 
news:ee%23H9$aZHHA.4000@TK2MSFTNGP02.phx.gbl...
>

> There is no problem passing a CWnd* to a thread and using it to post 
> messages.
>
> If it is destroyed, it is a programming bug.
>
> 

0
tom.nospam (3240)
3/13/2007 10:35:38 PM
Oops, great minds think alike again :o)

Tom

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:xqEJh.3031$Qw.1448@newssvr29.news.prodigy.net...
>

> PostMessage(hWnd,...);  will never crash. even if the window is long gone.
>
> pWnd->PostMessage(....) will crash if pWnd has been deleted.
>
> AliR.
>
> 

0
tom.nospam (3240)
3/13/2007 10:36:22 PM
This would work OK for a dialog, but in other kinds of doc/view applications 
this would be problematic of course.  Of course, you could have the 
mainframe handle the message even if it were not the final recipient.  There 
should always be at least the frame window while the program is running.

This has been an interesting discussion.

Tom

"David Ching" <dc@remove-this.dcsoft.com> wrote in message 
news:12ve7k03unjc1eb@news.supernews.com...
> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
> news:ON$ubCaZHHA.4692@TK2MSFTNGP04.phx.gbl...
>> Your object in a worker thread needs to be passed a CWnd* or HWND for the 
>> main window.  This can be passed into to the thread function via the 
>> thread parameter.
>>
>> Then you can use pwnd->PostMessage or ::PostMessage(hwnd,..) from your 
>> object.  Example here...
>> http://vcfaq.mvps.org/mfc/12.htm
>>
>
> Yes, except since it's the main dialog/window that is always being posted 
> to, the worker thread can just call AfxGetMainWnd() instead of being 
> passed it.
>
> -- David
> 

0
tom.nospam (3240)
3/13/2007 10:38:02 PM

Thanks for help,



I got it to work.  Everything seems to be fine.  I am able to post
messages to main gui thread produced inside user defined object.
However, I am a unsure of how and where is the appropriate place to
retrieve the main gui window handle.  Is it done inside the adapter
that converts C win32 api  to C++  and vice versa?  Or do I retrieve
handle before thread creation?

Currently, I am retreiving main gui window handle just before running
code for thread creation.  I tried retrieving handle using the code
below without success.

                CString a;
	CDummyDlg i;
	HWND h  =  i.m_hWnd;
	a.Format("i.m_hWnd %d", h);
	AfxMessageBox(a);

This code fails.  I don't understand why.  It always gets 0 as
handle.  What am I doing wrong?  The code listed below works .  I get
a valid window handle.  But why can't I retrieve the handle using
above method?  To me above code seems more logical.

                HWND mywin;
	mywin = ::FindWindow(0,"Dummy");
	a.Format("Window Handle %d", mywin);
	AfxMessageBox(a);

The other thing is I decalared mywin as a global variable.  Because
otherwise, I get failure inside my c->c++ adapter code.  It reports
that mywin is undeclared.

// Adapter code
UINT CDummyDlg::tMain(LPVOID p)
{

	CDummyDlg * self = (CDummyDlg *)p;
	self->tMain(mywin);	<<< Here it says undeclared variable if I don't
make mywin global variable.
	return 0;
}


If you guys eliminate some mysteries on how to appropriately construct
my code, that will help me become a better coder.  Thanks.

If you see two posts, please disregard one.

0
3/14/2007 1:00:49 PM
You want to make sure the dialog was created first by calling Create or 
DoModal().  I'd make the call to set the handle in the thread inside the 
OnInitDialog() function.

Tom

"one-trick-pony" <worldofpain.aamir@gmail.com> wrote in message 
news:1173877249.518150.45990@b75g2000hsg.googlegroups.com...
>
>
> Thanks for help,
>
>
>
> I got it to work.  Everything seems to be fine.  I am able to post
> messages to main gui thread produced inside user defined object.
> However, I am a unsure of how and where is the appropriate place to
> retrieve the main gui window handle.  Is it done inside the adapter
> that converts C win32 api  to C++  and vice versa?  Or do I retrieve
> handle before thread creation?
>
> Currently, I am retreiving main gui window handle just before running
> code for thread creation.  I tried retrieving handle using the code
> below without success.
>
>                CString a;
> CDummyDlg i;
> HWND h  =  i.m_hWnd;
> a.Format("i.m_hWnd %d", h);
> AfxMessageBox(a);
>
> This code fails.  I don't understand why.  It always gets 0 as
> handle.  What am I doing wrong?  The code listed below works .  I get
> a valid window handle.  But why can't I retrieve the handle using
> above method?  To me above code seems more logical.
>
>                HWND mywin;
> mywin = ::FindWindow(0,"Dummy");
> a.Format("Window Handle %d", mywin);
> AfxMessageBox(a);
>
> The other thing is I decalared mywin as a global variable.  Because
> otherwise, I get failure inside my c->c++ adapter code.  It reports
> that mywin is undeclared.
>
> // Adapter code
> UINT CDummyDlg::tMain(LPVOID p)
> {
>
> CDummyDlg * self = (CDummyDlg *)p;
> self->tMain(mywin); <<< Here it says undeclared variable if I don't
> make mywin global variable.
> return 0;
> }
>
>
> If you guys eliminate some mysteries on how to appropriately construct
> my code, that will help me become a better coder.  Thanks.
>
> If you see two posts, please disregard one.
> 

0
tom.nospam (3240)
3/14/2007 2:08:35 PM
It's not luck that it has worked in for you because 99.9% of times the CWnd 
* will stay valid during the life of the thread. it's that 0.1% that might 
get you in trouble, which could have been prevented in the first place.

It's like me getting used to putting _T() around my string literals.  My app 
is not Unicode, and I'm not planning on switching to it, but it's a good 
habit to pickup.

AliR.

"Tom Serface" <tom.nospam@camaswood.com> wrote in message 
news:O3HyJ$bZHHA.1220@TK2MSFTNGP03.phx.gbl...
> Hi Ali,
>
> That's a good point.  I've never had trouble setting a CWnd pointer in 
> thread code.  I typically call the function in the object before the 
> thread is started with something like:
>
> m_pMyThread.SetStatusWnd(CWnd *)
>
> but your point seems logical.  Maybe I've just been lucky in using it a 
> way it works.  One thing that is worth noting is that if the window that 
> the CWnd * points to goes away (is closed) a call like 
> m_StatusWnd->SendMessage() will cause a failure whereas the call to 
> SendMessage() with an handle will simple be ignored.
>
> Tom
>
> "AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
> news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>>
>> I would pass the HWND instead of the CWnd, you never know CWnd my not be 
>> thread safe. Or the CWnd object might get destroyed without the thread 
>> knowing about it.
>>
>> AliR.
>>
>>
> 


0
AliR3470 (3236)
3/14/2007 3:00:13 PM
That all depends on where you are creating the thread. If your dialog is a 
modal dialog then you will have to create your thread from within the 
OnInitDialog method, that's the first time you will know about the dialog's 
HWND.  If it's a modeless dialog then right after you call Create you can 
access it's HWND.

AliR.

"one-trick-pony" <worldofpain.aamir@gmail.com> wrote in message 
news:1173877249.518150.45990@b75g2000hsg.googlegroups.com...
>
>
> Thanks for help,
>
>
>
> I got it to work.  Everything seems to be fine.  I am able to post
> messages to main gui thread produced inside user defined object.
> However, I am a unsure of how and where is the appropriate place to
> retrieve the main gui window handle.  Is it done inside the adapter
> that converts C win32 api  to C++  and vice versa?  Or do I retrieve
> handle before thread creation?
>
> Currently, I am retreiving main gui window handle just before running
> code for thread creation.  I tried retrieving handle using the code
> below without success.
>
>                CString a;
> CDummyDlg i;
> HWND h  =  i.m_hWnd;
> a.Format("i.m_hWnd %d", h);
> AfxMessageBox(a);
>
> This code fails.  I don't understand why.  It always gets 0 as
> handle.  What am I doing wrong?  The code listed below works .  I get
> a valid window handle.  But why can't I retrieve the handle using
> above method?  To me above code seems more logical.
>
>                HWND mywin;
> mywin = ::FindWindow(0,"Dummy");
> a.Format("Window Handle %d", mywin);
> AfxMessageBox(a);
>
> The other thing is I decalared mywin as a global variable.  Because
> otherwise, I get failure inside my c->c++ adapter code.  It reports
> that mywin is undeclared.
>
> // Adapter code
> UINT CDummyDlg::tMain(LPVOID p)
> {
>
> CDummyDlg * self = (CDummyDlg *)p;
> self->tMain(mywin); <<< Here it says undeclared variable if I don't
> make mywin global variable.
> return 0;
> }
>
>
> If you guys eliminate some mysteries on how to appropriately construct
> my code, that will help me become a better coder.  Thanks.
>
> If you see two posts, please disregard one.
> 


0
AliR3470 (3236)
3/14/2007 3:03:07 PM
one-trick-pony wrote:
> Currently, I am retreiving main gui window handle just before running
> code for thread creation.  I tried retrieving handle using the code
> below without success.
> 
>                 CString a;
> 	CDummyDlg i;
> 	HWND h  =  i.m_hWnd;
> 	a.Format("i.m_hWnd %d", h);
> 	AfxMessageBox(a);
> 
> This code fails.  I don't understand why.  It always gets 0 as
> handle.  What am I doing wrong?  

A CWnd object such as CDummyDlg does not have a window (so it does not 
have a valid m_hWnd) when it is constructed.  It only has a window and 
valid m_hWnd after its Create (or DoModal) function is called.  The 
design of MFC requires this "two stage construction".



The code listed below works .  I get
> a valid window handle.  But why can't I retrieve the handle using
> above method?  To me above code seems more logical.
> 
>                 HWND mywin;
> 	mywin = ::FindWindow(0,"Dummy");
> 	a.Format("Window Handle %d", mywin);
> 	AfxMessageBox(a);
> 

Your thread depends on the HWND, so the thread should not be created 
until the HWND is available.  That happens in OnInitDialog.  So create 
the thread there.  Before you create it set mywin = m_hWnd.


> The other thing is I decalared mywin as a global variable.  Because
> otherwise, I get failure inside my c->c++ adapter code.  It reports
> that mywin is undeclared.
> 
> // Adapter code
> UINT CDummyDlg::tMain(LPVOID p)
> {
> 
> 	CDummyDlg * self = (CDummyDlg *)p;
> 	self->tMain(mywin);	<<< Here it says undeclared variable if I don't
> make mywin global variable.
> 	return 0;
> }
> 
> 
> If you guys eliminate some mysteries on how to appropriately construct
> my code, that will help me become a better coder.  Thanks.
> 
> If you see two posts, please disregard one.
> 

You created mywin as a local variable.  That means it does not exist 
outside the function where it was created.  See C++ "scope" rules.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/14/2007 3:26:42 PM
I can't argue with that logic and I plan to change my ways.  You can teach 
an old MFC dog new tricks :o)

Tom

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:16UJh.5382$Um6.246@newssvr12.news.prodigy.net...
> It's not luck that it has worked in for you because 99.9% of times the 
> CWnd * will stay valid during the life of the thread. it's that 0.1% that 
> might get you in trouble, which could have been prevented in the first 
> place.
>
> It's like me getting used to putting _T() around my string literals.  My 
> app is not Unicode, and I'm not planning on switching to it, but it's a 
> good habit to pickup.
>
> AliR.

0
tom.nospam (3240)
3/14/2007 3:28:24 PM
If you have got a thread posting messages to a window which may have been 
destroyed, you have a design problem.

You should redesign your logic so that you post to objects that exist. If 
the view may have been destroyed, post to the main window and let it decide 
what is required.

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:16UJh.5382$Um6.246@newssvr12.news.prodigy.net...
> It's not luck that it has worked in for you because 99.9% of times the 
> CWnd * will stay valid during the life of the thread. it's that 0.1% that 
> might get you in trouble, which could have been prevented in the first 
> place.
>
> It's like me getting used to putting _T() around my string literals.  My 
> app is not Unicode, and I'm not planning on switching to it, but it's a 
> good habit to pickup.
>
> AliR.
>
> "Tom Serface" <tom.nospam@camaswood.com> wrote in message 
> news:O3HyJ$bZHHA.1220@TK2MSFTNGP03.phx.gbl...
>> Hi Ali,
>>
>> That's a good point.  I've never had trouble setting a CWnd pointer in 
>> thread code.  I typically call the function in the object before the 
>> thread is started with something like:
>>
>> m_pMyThread.SetStatusWnd(CWnd *)
>>
>> but your point seems logical.  Maybe I've just been lucky in using it a 
>> way it works.  One thing that is worth noting is that if the window that 
>> the CWnd * points to goes away (is closed) a call like 
>> m_StatusWnd->SendMessage() will cause a failure whereas the call to 
>> SendMessage() with an handle will simple be ignored.
>>
>> Tom
>>
>> "AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
>> news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>>>
>>> I would pass the HWND instead of the CWnd, you never know CWnd my not be 
>>> thread safe. Or the CWnd object might get destroyed without the thread 
>>> knowing about it.
>>>
>>> AliR.
>>>
>>>
>>
>
> 
0
anyone3666 (139)
3/14/2007 7:54:00 PM
Please explain why that is a design problem?

AliR.

"Ian Semmel" <anyone@rocketcomp.com.au> wrote in message 
news:OmUgBKnZHHA.1400@TK2MSFTNGP06.phx.gbl...
> If you have got a thread posting messages to a window which may have been 
> destroyed, you have a design problem.
>
> You should redesign your logic so that you post to objects that exist. If 
> the view may have been destroyed, post to the main window and let it 
> decide what is required.
>
> "AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
> news:16UJh.5382$Um6.246@newssvr12.news.prodigy.net...
>> It's not luck that it has worked in for you because 99.9% of times the 
>> CWnd * will stay valid during the life of the thread. it's that 0.1% that 
>> might get you in trouble, which could have been prevented in the first 
>> place.
>>
>> It's like me getting used to putting _T() around my string literals.  My 
>> app is not Unicode, and I'm not planning on switching to it, but it's a 
>> good habit to pickup.
>>
>> AliR.
>>
>> "Tom Serface" <tom.nospam@camaswood.com> wrote in message 
>> news:O3HyJ$bZHHA.1220@TK2MSFTNGP03.phx.gbl...
>>> Hi Ali,
>>>
>>> That's a good point.  I've never had trouble setting a CWnd pointer in 
>>> thread code.  I typically call the function in the object before the 
>>> thread is started with something like:
>>>
>>> m_pMyThread.SetStatusWnd(CWnd *)
>>>
>>> but your point seems logical.  Maybe I've just been lucky in using it a 
>>> way it works.  One thing that is worth noting is that if the window that 
>>> the CWnd * points to goes away (is closed) a call like 
>>> m_StatusWnd->SendMessage() will cause a failure whereas the call to 
>>> SendMessage() with an handle will simple be ignored.
>>>
>>> Tom
>>>
>>> "AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
>>> news:b1DJh.3013$Qw.2264@newssvr29.news.prodigy.net...
>>>>
>>>> I would pass the HWND instead of the CWnd, you never know CWnd my not 
>>>> be thread safe. Or the CWnd object might get destroyed without the 
>>>> thread knowing about it.
>>>>
>>>> AliR.
>>>>
>>>>
>>>
>>
>> 


0
AliR3470 (3236)
3/14/2007 8:03:21 PM
AliR (VC++ MVP) wrote:
> 
> "Ian Semmel" <anyone@rocketcomp.com.au> wrote in message 
> news:OmUgBKnZHHA.1400@TK2MSFTNGP06.phx.gbl...
> 
>>If you have got a thread posting messages to a window which may have been 
>>destroyed, you have a design problem.
>>
>>You should redesign your logic so that you post to objects that exist. If 
>>the view may have been destroyed, post to the main window and let it 
>>decide what is required.
>>
> Please explain why that is a design problem?
> 
> AliR.

Permitting a thread to execute using invalid information is a problem. 
Obvious case: Giving the thread a pointer, then letting the pointee be 
destroyed while the thread is still running.  Giving the thread an HWND 
that becomes invalid is just as wrong, even if the end result might be 
less catastrophic.  (Where does the documentation promise that you can 
use invalid HWNDs?  Why would you want to?)

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/14/2007 8:40:48 PM
I agree that sending a message to an Invalid hWnd is not in the 
documentation anywhere.  And I never said that supplying a bad pointer or 
hwnd to a thread is good.  All I am saying is that it's better for the 
thread to get a HWND instead of a CWnd * for the fact that if the window 
desides to disappear for any reason (for reasons that I can think of) the 
thread will not crash posting messages to an invalid hwnd but will crash 
with an invalid CWnd *.


AliR.


"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message 
news:egafIknZHHA.4520@TK2MSFTNGP06.phx.gbl...
> AliR (VC++ MVP) wrote:
>>
>> "Ian Semmel" <anyone@rocketcomp.com.au> wrote in message 
>> news:OmUgBKnZHHA.1400@TK2MSFTNGP06.phx.gbl...
>>
>>>If you have got a thread posting messages to a window which may have been 
>>>destroyed, you have a design problem.
>>>
>>>You should redesign your logic so that you post to objects that exist. If 
>>>the view may have been destroyed, post to the main window and let it 
>>>decide what is required.
>>>
>> Please explain why that is a design problem?
>>
>> AliR.
>
> Permitting a thread to execute using invalid information is a problem. 
> Obvious case: Giving the thread a pointer, then letting the pointee be 
> destroyed while the thread is still running.  Giving the thread an HWND 
> that becomes invalid is just as wrong, even if the end result might be 
> less catastrophic.  (Where does the documentation promise that you can use 
> invalid HWNDs?  Why would you want to?)
>
> -- 
> Scott McPhillips [VC++ MVP]
> 


0
AliR3470 (3236)
3/14/2007 9:05:05 PM
Hi Ali,

I think Ian is just saying that if the window could go away you should know 
that it happened and design it to work regardless (not to just crash).  Of 
course, using the HANDLE method it wouldn't crash anyway, it just wouldn't 
work if the window goes away for some reason.  In my experience, if the 
window goes away I have bigger problems.

Tom

"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message 
news:dyYJh.5613$Um6.3136@newssvr12.news.prodigy.net...
> Please explain why that is a design problem?
>
> AliR.

0
tom.nospam (3240)
3/14/2007 11:57:34 PM
Greetings,

Scott you said,

A CWnd object such as CDummyDlg does not have a window (so it does
not
have a valid m_hWnd) when it is constructed.  It only has a window
and
valid m_hWnd after its Create (or DoModal) function is called.  The
design of MFC requires this "two stage construction".

How can I make my code wait/suspend until Create or DoModal has run?
That is how can I provide mechanism in my code to suspend thread
creation or further code execution until a valid HWND is available?
How can I check if it has run?

Furthermore:

You created 'mywin 'as a local variable.  That means it does not
exist
outside the function where it was created.  See C++ "scope" rules.


Initially, I had declared 'mywin' to be of HWND datatype inside
CDummyDlg class declaration as Private data member.  To my
understanding all member functions should have access to Private data
members in same class.   I would get an error message about something
being static inside adapter code(sorry can't remember exact message).
Everything is part of CDummyDlg, including thread creation functions.
Since thread functions are also part of same class as 'mywin' they
should be able to access it.  I tried making 'mywin' Public still I
was getting an error.  Finally, I took it and placed it inside the
CDummyDlg::Run() function  (which actually creates threads).
Therefore, 'mywin'  was local to that function and I was passing it to
thread code.  But it would fail.  As I understand, it doesnt matter if
I pass a datatype which is local because point is as long as I have
valid data to pass-it should work in thread code.  Thats where I was
getting undeclared variable message in adapter code.  Then I made
'mywin' global and everything worked like a charm.  So it was trial
and error but I want to understand good engineering principles.

0
3/15/2007 1:52:43 AM
The best way to do it is to just start the thread in the OnInitDialog() 
function in the dialog code.  This is especially true if it is a dialog 
application, otherwise, you'd have to run the dialog as a non-modal dialog 
and just wait for the return from the call to Create() before assigning the 
window to the thread object.

Tom

"one-trick-pony" <worldofpain.aamir@gmail.com> wrote in message 
news:1173923563.076554.293090@p15g2000hsd.googlegroups.com...
> Greetings,
>
> Scott you said,
>
> A CWnd object such as CDummyDlg does not have a window (so it does
> not
> have a valid m_hWnd) when it is constructed.  It only has a window
> and
> valid m_hWnd after its Create (or DoModal) function is called.  The
> design of MFC requires this "two stage construction".
>
> How can I make my code wait/suspend until Create or DoModal has run?
> That is how can I provide mechanism in my code to suspend thread
> creation or further code execution until a valid HWND is available?
> How can I check if it has run?
>
> Furthermore:
>
> You created 'mywin 'as a local variable.  That means it does not
> exist
> outside the function where it was created.  See C++ "scope" rules.
>
>
> Initially, I had declared 'mywin' to be of HWND datatype inside
> CDummyDlg class declaration as Private data member.  To my
> understanding all member functions should have access to Private data
> members in same class.   I would get an error message about something
> being static inside adapter code(sorry can't remember exact message).
> Everything is part of CDummyDlg, including thread creation functions.
> Since thread functions are also part of same class as 'mywin' they
> should be able to access it.  I tried making 'mywin' Public still I
> was getting an error.  Finally, I took it and placed it inside the
> CDummyDlg::Run() function  (which actually creates threads).
> Therefore, 'mywin'  was local to that function and I was passing it to
> thread code.  But it would fail.  As I understand, it doesnt matter if
> I pass a datatype which is local because point is as long as I have
> valid data to pass-it should work in thread code.  Thats where I was
> getting undeclared variable message in adapter code.  Then I made
> 'mywin' global and everything worked like a charm.  So it was trial
> and error but I want to understand good engineering principles.
> 

0
tom.nospam (3240)
3/15/2007 2:51:09 AM
one-trick-pony wrote:
> Greetings,
> 
> Scott you said,
> 
> A CWnd object such as CDummyDlg does not have a window (so it does
> not
> have a valid m_hWnd) when it is constructed.  It only has a window
> and
> valid m_hWnd after its Create (or DoModal) function is called.  The
> design of MFC requires this "two stage construction".
> 
> How can I make my code wait/suspend until Create or DoModal has run?
> That is how can I provide mechanism in my code to suspend thread
> creation or further code execution until a valid HWND is available?
> How can I check if it has run?

Create or DoModal are part of your code: You can't suspend your code to 
wait until your code does something!

The only way to create the thread after the HWND is to put the thread 
creation code at the proper spot in the execution flow. That spot is 
OnInitDialog.

> 
> Furthermore:
> 
> You created 'mywin 'as a local variable.  That means it does not
> exist
> outside the function where it was created.  See C++ "scope" rules.
> 
> 
> Initially, I had declared 'mywin' to be of HWND datatype inside
> CDummyDlg class declaration as Private data member.  To my
> understanding all member functions should have access to Private data
> members in same class.   I would get an error message about something
> being static inside adapter code(sorry can't remember exact message).
> Everything is part of CDummyDlg, including thread creation functions.
> Since thread functions are also part of same class as 'mywin' they
> should be able to access it.  

Not quite.  The thread function is required to be a static function (so 
Window's C code can call it).  But a static function cannot access the 
members of a class (unless those members are also static).

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/15/2007 3:29:05 AM
Hello,

The DoModal function runs in MFC generated class CDummyApp in
InitInstance().

BOOL CDummyDlg::OnInitDialog()
{
....
How do I get window handle here?  What function and coding will allow
me to get the window handle?

PostMessage(UWM_AutoRun);
....
return TRUE;
}

CWnd *p_UIWnd = what?  How can I get handle of the window using CWnd*?

I know below line of code is inaccurate because I got error during
compile.


CWnd *p_UIWnd = CDummyDlg.m_hWnd;


or in HWND case (which works):

HwND hDummy = CDummyDlg::m_hWnd;

This works too:
HWND hDmmy = AfxGetMainWnd()->m_hWnd;

I am interested in knowing how to use CWnd type object. Thanks for
help.


0
3/15/2007 6:31:06 PM
one-trick-pony wrote:
> Hello,
> 
> The DoModal function runs in MFC generated class CDummyApp in
> InitInstance().
> 
> BOOL CDummyDlg::OnInitDialog()
> {
> ...
> How do I get window handle here?  What function and coding will allow
> me to get the window handle?

   HWND hDummy = m_hWnd;

m_hWnd is a member variable of CWnd.  CDummyDlg is a CWnd, so it 
inherits the m_hWnd.


> PostMessage(UWM_AutoRun);
> ...
> return TRUE;
> }
> 
> CWnd *p_UIWnd = what?  How can I get handle of the window using CWnd*?
> 
> I know below line of code is inaccurate because I got error during
> compile.
> 
> 
> CWnd *p_UIWnd = CDummyDlg.m_hWnd;
> 
> 
> or in HWND case (which works):
> 
> HwND hDummy = CDummyDlg::m_hWnd;

These are both bad C++.  The second one works because the "CDummyDlg::" 
is redundant -- unneeded.

> 
> This works too:
> HWND hDmmy = AfxGetMainWnd()->m_hWnd;
> 
> I am interested in knowing how to use CWnd type object. Thanks for
> help.
> 
> 

It is very confusing to try and learn C++ and MFC and Windows 
programming all at the same time.  Doing more C++ exercises from a C++ 
book would probably help you a lot.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/15/2007 8:12:42 PM
I study for sure.  I have always books open.  From basic C++ theory to
MFC and Windows API and MSDN website.  I have learned alot in past
few  months.  Sometimes details end up confusing me.  I have come to
love see my code interact with real systems, making CD Rom door Open,
checking network status, using files, using threads, setting up
registery and list goes on and on.  I am far from perfect but I want
to learn.  I used to get scared seeing anything above 50 lines of
code.  Now I have written code that is thousands of line and even
spans over multiple files.  So motivation to learn is definitely
there.  No one book or number of books can cover everything.  It is
through exposure to material, asking questions and practicing is what
is gonna fill in the gaps.  I can read theory day and night. Now its
practice time.  Like the two methods I showed here, my question is are
they valid? For all I know, in my knowledge I can be a happy camper
and just assume it the best or close to best code that I have
written.  I would like you folks to help me spot error of my ways.
Having said that, even though I do get valid window handle , is this
good software engineering approach?

1> HWND hDummy = hWnd;

2>HWND hDmmy = AfxGetMainWnd()->m_hWnd;

To me they work so I assume this code is good.  But someone  with
experience will look at this and find this to be the most pathetic
code and/or there is a better way to do this.  Lastly, CWnd, I am
using code below to get window handle-it works.

CWnd* winh = this;
s.Format("%i", winh->m_hWnd);
AfxMessageBox(s);

Again, is this good software engineering approach?

0
3/16/2007 7:00:04 PM
one-trick-pony wrote:
> Having said that, even though I do get valid window handle , is this
> good software engineering approach?
> 
> 1> HWND hDummy = hWnd;
> 
> 2>HWND hDmmy = AfxGetMainWnd()->m_hWnd;
> 
> To me they work so I assume this code is good.  But someone  with
> experience will look at this and find this to be the most pathetic
> code and/or there is a better way to do this.  Lastly, CWnd, I am
> using code below to get window handle-it works.
> 
> CWnd* winh = this;
> s.Format("%i", winh->m_hWnd);
> AfxMessageBox(s);
> 
> Again, is this good software engineering approach?

There's no software engineering here.  In your original problem you 
needed the dialog's HWND, and m_hWnd has what you need.  You're just 
copying a variable that you already have, unnecessarily.  That's just 
playing with the language.  So enjoy :)

-- 
Scott McPhillips [VC++ MVP]

0
Scott
3/16/2007 11:04:09 PM
Awesome, I see whats happening. Thanks for help along the way.

0
3/17/2007 4:04:24 AM
Reply:

Similar Artilces:

MFC and threads #3
Greetings, I am running into a problem when trying to pause a thread in MFC based application. This is my first time using user defined Events for synchronization. The scenario is like this. Code executes in a worker thread and based on some conditions thread launches another new dialog window. That window displays some message to user ie, Hello There! in an Edit control. I provide a Resume button on the bottom of that message Window so that user can continue after reading the message and the worker thread should continue from where it left off. This is the code for that Resume button. v...

defining accounts receivable default accounts
We would like the ability to have limitations on the accounts that can be selected for accounts receviable for a sales order transaction or receivable transaction. Currently you can select any account number for the RECEV trx type. We would like to define allowable accounts receivable accounts to avoid our current out of balance condition. This would limit the possibilities of reasons for ar trade to be out of balance to gl. This is a very hot issue at many clients in trying to reconcile ar to gl. This could also apply to AP!! Thanks, lori b. ---------------- This post is a sug...

Passing Outlook objects between threads C++
Im using C++ VS2005 and in the main thread I establish a reference to an Outlook Contact Item. I call the Display method and it correctly creates an inspector and displays the Contact. While the Contact is open I pass a IDISPATCH pointer to the Contact to another thread. In the new thread I AddRef the Contact, the pointer seems good because I can get various properties, FileAs etc. However when I try to call the Close method I get an invalid access exception (the method does not return with an hr result). Watching the Inspector events, OnClose and OnCloseComplete do get called before ...

Multiple user names at one bank or brokerage
I have 3 accounts at one bank. I have 3 corresponding accounts set up in MS Money 2007. One of them is setup for online banking while the other two are not. I now want to set the other two up for online banking but I can't find a way to specify the unique login info for each of the other two accounts. ...

instructions disppear when users begin type (text field)
Hi all, I need to customize the outlook contact form and I want to add one text field to allow users to add details info and instruct users how to add. Instructions shows in the field and the instructions disappear when users click and begin to type. How should I do this? exchange 2003/outlook2003 Thank you. It's hard for me to visualize exactly what you're expecting to happen. If you want the instructions to stay on the screen, you could display them in a label control. -- Sue Mosher, Outlook MVP Author of Microsoft Outlook 2007 Programming: Jumps...

Strange Problem when closing the database object
Hi All, I have a COM component using which is interacting with the database using the CDAODATABASE class. my applications works fine when i built it with VS6. when i built the application with VS2005, the application is suddently unloaded when i am trying to close the database. my database object is CDaoDatabase *m_DB; when my application calls the following method the component is getting unloaded. STDMETHODIMP CFeatureTable::Close() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) HRESULT hRes = S_OK; if (!m_Initialized) return S_OK; TRY_DAO { // Nee...

Alert user to check the filed when it is final
Hi, I want to create an alert to check the Final( checkbox ) field, if date_issued is enter. if date_issued is null then do nothing, In what even I have to code this and how? -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-formscoding/201001/1 mls via AccessMonster.com wrote: >Hi, I want to create an alert to check the Final( checkbox ) field, if >date_issued is enter. >if date_issued is null then do nothing, In what even I have to code this and >how? Based on just that one requirement, you can use the date text b...

viewing a user's email account
I have been charged with reviewing a user's email activity / mailbox for a possible impropriety. Is there a way to do this from exchange, or better yet what is the best way to accomplish this steathfuly? On Mon, 23 Oct 2006 18:20:02 -0700, Rossel1 <Rossel1@discussions.microsoft.com> wrote: >I have been charged with reviewing a user's email activity / mailbox for a >possible impropriety. Is there a way to do this from exchange, or better yet >what is the best way to accomplish this steathfuly? I really hope that the users' employment contract provides for this ...

switch users without closing program
Hi, Is there a way to switch profiles without closing Outlook 2002? Thanks, Duane ...

MFC get drive information
Dear all, How to get drive information of my computer? I want to get available driver letters and the corresponding disk space information. Thanks! Best Regards, Crystal Use GetLogicalDrives() to get drive informatio. Use GetDiskFreeSpace(... to get drive space. Sekhar Hyderabad. "Crystal" wrote: > Dear all, > > How to get drive information of my computer? > > I want to get available driver letters and the corresponding disk space > information. > > Thanks! > > Best Regards, > Crystal > > > > > You can use cha...

Excel object within Word doc
I am sure I am doing something in error ... but all I want to do is take a spreadsheet and embed it in my word doc. I click on 'Insert', then 'Object', choose 'Excel 97-2003', 'Display as Icon', copy data into spreadsheet, 'save', 'close and return' ... However, when I go to view it at a later time - the spreadsheet does not open and I end up in the picture editing part of Word. ...

reconnect to mailbox after Disabling user?
Hello, I was wondering if there is a easier way then to recycle the Exchange Services to renable a mailbox after a users mailbox has been disabled for x amount of time. I want to give access to this mailbox to another user so right now.. I simply go to Mailbox Rights -> (add the user I want to add) -> give allow rights to Read / Full but this requires a recycle of the services. Is there another way without recycling the services? G. On Mon, 23 Aug 2004 11:08:36 -0400, "Just Another Admin" <just@dmin.net> wrote: >Hello, > >I was wondering if there ...

How to let OWA users to change their password ?
How to let OWA users to change their password ? On Wed, 9 Nov 2005 01:58:03 -0800, "Enid" <Enid@discussions.microsoft.com> wrote: >How to let OWA users to change their password ? There are quite a few steps but if you follow this: http://support.microsoft.com/default.aspx?scid=kb;en-us;297121 carefuly, you'll be ok. Thanks for your information. Follow the document to enable the change password funcation, but got error when using IE6 w/ SP2 to do change password "error number : 5". It is so tricky if using FireFox, the password change is work. Any suggesti...

Cannot add user to CRM
I have trouble adding user to the CRM. I have 50 users in AD, however, when it comes to adding user in CRM, I can only choose from 15 of them. I wonder why!? I suppose I should be able to pick from all 50 users whoever is in the AD, right? If anyone have any clue on this problem and give me some feedback, I would surely appreciate that!!! CRM deployment manager will not display certain categories of users - eg if they have been migrated from NT domains or Novel NDS without passwords. Search the group archives for UserAccountControl "Martijn van Halen" <MartijnvanHalen@d...

User authentication/protection
I'm a professor and use Excel extensively in my classes. I need to be able to assign Excel projects to my students and have some increased assurance that the students have submitted their own work. I've tried my hand at VBA programming and also simply used track changes. None of these solutions (even the track changes) worked reliably across both mac and pc platforms. I would like some advice about whether what I need from Excel is even possible. I Here's what I need and what I think would work: 1. I'd like to prevent a student from getting someone else's sol...

RMS Integration user access
How can I give users in GP access to the RMS Import (Transactions > Retail Management > RM Import) function? I found KB 900636, but it did not work. I'm using GP10 and HQ & RMS 2.0. Thanks, Jocelyn ...

how to capture the button caption using an object
How to capture the button caption using an object in Dialog based MFC ..Suggest me which method to use. iam in urgent need of it. please give reply as soon as possible Thank you. What do you mean "capture the button caption"? Do you mean "Read the caption on a button"? GetWindowText works fine for this. Create a control variable and use it as CString s; c_MyButton.GetWindowText(s); and you've got it. joe On 7 Aug 2006 22:08:50 -0700, "lakshmi" <lakshmi.bollam@gmail.com> wrote: >How to captu...

Select more than 1 object in the lookup field
I would like to have the ability to select more than one object in a lookup field. Is there a way to accomplish this? Thanks No. The only exception to this is the party list (like the to, cc and bcc fields in an email), but this is an internal thing and cannot be used in customizations. -- Michael H�hne, Microsoft Dynamics CRM MVP Web: http://www.stunnware.com/crm2 Feed: http://www.stunnware.com/crm2/atom.aspx Custom Lookup Dialog: http://www.stunnware.com/crm2/?area=customLookup ---------------------------------------------------------- "Onetreeup" <Onetreeup@discussions...

OWA Delegate to non-Exchange user?
Hi all, is it possible to delegate OWA access to a user account that doesn't use Exchange? If I try to set up a delegate from my Outlook 2003 client it requests a mail id rather that a login ID. I've tried giving the account permissions in AD U and C but test show that the pasword for the delegate isn't accepted when trying to log in to OWA. Could anyone advise please. Regards Graeme <graeme_smith77@hotmail.com> wrote in message news:1132923835.303433.156340@g43g2000cwa.googlegroups.com... > Hi all, > is it possible to delegate OWA access to a user account that ...

Objects duplicated
If a workbook consists of Objects: Sheet1, Sheet2, This Workbook.... What would cause Excel to duplicate these objects in the same workbook. Somehow, I now have to sets of objects in the same workbook that are exactly the same. ...

Creating an OWA access only user.
Hello chaps/ladies, The pointy hairs have asked that some one external to our organisation be given access to an email account within our organisation. So my thoughts were to create a user account and lock it down to prevent interactive access to any workstation machines in any way and only allow OWA access, this has prroven trickier than I first assumed. Does anyone have a quick guide going over best practices for this type of situation? TIA. -- Alex Griffin On Tue, 14 Jun 2005 16:26:11 +0100, Alex Griffin <nntpuser@hotmail.com> wrote: >Hello chaps/ladies, > >The pointy...

user shares vs. folder redirection
Just finished a brute force migration of SBS 2003 to 2008 and I am wondering if there is any reason why I would want to place the old user shares files into the new user shares folder. I should have prefaced by saying that the only reason they used the user shares in sbs 03 is because someone never setup redirection. Instead just told them to copy everything into their ..lnk folder. So I am thinking since these are not files that they cared to share with others anyway why can't I just copy them into their redirected folders? I would just put the files wherever the u...

granting users Send as permissions causes errors unless they select the FROM address from the GAL
I've got some users who have two mailboxes, each with a different address. They need to be able to select which address they send from, on occasion. I've given them "send on behalf" and "send as" permissions the 2nd accounts. But, unless they select the FROM address from the GAL, they get the following error: You do not have permission to send to this recipient. If they manually type in the address, they receive this error. This is only a problem because I would prefer to hide the 2nd account from the GAL, to avoid confusion. Does anyone know a way around this?...

What security setting is it that allows a user to change
Hi, What is the security setting that allows a user to change a Great Plains Form thus creating a forms.dic file on the server? We are using Great Plains 7.5, citrix servers on windows 2000. I am not sure the security is in Great Plains, it is probably on the citrix box but I am not sure. Any help would be greatly appreciated. Thanks, Connie Connie You can turn off access to the Modifier using either standard security (Setup >> System >> Security), or Advanced Security (Setup >> System >> Advanced Security). I would use Advanced Security as it is a more po...

switch users for outlook
is there a way to set-up multiple users and then use switch user (like in outlook express) for outlook 2000? thanks in advance for your help Hi, Ofcourse you can do it ifyou are working in the Corporate/Workgroup Mode. Even if you are in the Internet Mail Only Mode, Goto Tools --> Options --> Mail Delivery Tab Click on the Reconfigure mail support and then select the Corporate WorkGroup. Note: You need to have the Office 2000 CD to switch the modes. Once you are in the Corporate Workgroup Mode, please do the follwoing. Goto the Control Panel and then doub...