Discussion:
passing "parent" ID to a new thread
(too old to reply)
r***@yahoo.com
2007-07-04 20:03:41 UTC
Permalink
Hi all,
From the main window I create a new thread that is supposed to open a
secondary window and to communicate with the main window by
PostMessage.

pNewThread = (CNewThread*)AfxBeginThread(RUNTIME_CLASS(CNewThread),0);

I need to pass the ID of the main window to the new thread when I
create the new thread, to be able to PostMessage to the main window
later. How do I pass the main widnow ID to the new thread? I
understand that there is no parent-child reglationship between threads.
Doug Harrison [MVP]
2007-07-04 20:21:09 UTC
Permalink
Post by r***@yahoo.com
Hi all,
From the main window I create a new thread that is supposed to open a
secondary window and to communicate with the main window by
PostMessage.
pNewThread = (CNewThread*)AfxBeginThread(RUNTIME_CLASS(CNewThread),0);
I need to pass the ID of the main window to the new thread when I
create the new thread, to be able to PostMessage to the main window
later. How do I pass the main widnow ID to the new thread? I
understand that there is no parent-child reglationship between threads.
If there were such a relationship, it wouldn't be between a window and a
thread; it would be between the threads. For a number of reasons, it is
usually better for one thread to create all the windows; this thread is
called the "main UI thread" among other things. That said, to pass
information into a thread when you create it, use the second AfxBeginThread
parameter for which you're currently passing 0. If it's pointer-sized or
smaller, you can just cast it to LPVOID when you call AfxBeginThread and
back to the original type in the thread entry function; it's common to do
this with the "this" pointer of a member function. If you need to pass a
lot of information and can't do it with "this", create a new class or
struct, dynamically create an instance with new, and pass a pointer to it
as the thread parameter. The created thread then assumes ownership of the
object and must delete it when done with it. This hand-off protocol avoids
lifetime issues, and you may need to use it when you PostMessage between
threads. Finally, for some info on safely using AfxBeginThread, see:

http://members.cox.net/doug_web/threads.htm
--
Doug Harrison
Visual C++ MVP
r***@yahoo.com
2007-07-04 20:30:04 UTC
Permalink
Doug,

Thanks a bunch, it hepled a lot.
Ben Voigt [C++ MVP]
2007-07-05 02:05:53 UTC
Permalink
Post by r***@yahoo.com
Hi all,
From the main window I create a new thread that is supposed to open a
secondary window and to communicate with the main window by
PostMessage.
pNewThread = (CNewThread*)AfxBeginThread(RUNTIME_CLASS(CNewThread),0);
I need to pass the ID of the main window to the new thread when I
create the new thread, to be able to PostMessage to the main window
later. How do I pass the main widnow ID to the new thread? I
understand that there is no parent-child reglationship between threads.
If there's only one "main window", then a global variable will work
perfectly fine.
r***@yahoo.com
2007-07-05 02:50:39 UTC
Permalink
Post by Ben Voigt [C++ MVP]
global variable
I know, this is how I've been doing it so far, but I am drowning in
globals by now... too many of them. I am trying to make it cleaner.
Ben Voigt [C++ MVP]
2007-07-05 03:13:27 UTC
Permalink
Post by r***@yahoo.com
Post by Ben Voigt [C++ MVP]
global variable
I know, this is how I've been doing it so far, but I am drowning in
globals by now... too many of them. I am trying to make it cleaner.
static member variable... to keep things organized

But it still acts like a global.
Scott Seligman [MSFT]
2007-07-05 04:27:48 UTC
Permalink
Post by r***@yahoo.com
Hi all,
From the main window I create a new thread that is supposed to open a
secondary window and to communicate with the main window by
PostMessage.
pNewThread = (CNewThread*)AfxBeginThread(RUNTIME_CLASS(CNewThread),0);
I need to pass the ID of the main window to the new thread when I
create the new thread, to be able to PostMessage to the main window
later. How do I pass the main widnow ID to the new thread? I
understand that there is no parent-child reglationship between threads.
Why not pass it in as the pParam of AfxBeginThread()?
--
Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.
r***@yahoo.com
2007-07-06 04:34:01 UTC
Permalink
Post by Scott Seligman [MSFT]
Why not pass it in as the pParam of AfxBeginThread()?
Yeah, I am trying to figure it out, but I am confused about converting
"this" (which is a pointer to a CDialog class) to LPVOID. If I just
do

AfxBeginThread(RUNTIME_CLASS(Fluoro_532_thread), this)


...it does not work, and casting "this" to (void *) does not work
either.
r***@yahoo.com
2007-07-06 04:50:04 UTC
Permalink
From the MSDN AfxBeginThread description is appers that overloading
AfxBeginThread with RUNTIME_CLASS(thread) as the first parameter
creates a user interface thread. "LPVOID pParameter" is not among
parameters of an interface thread overloading.
Scott McPhillips [MVP]
2007-07-06 11:33:17 UTC
Permalink
Post by r***@yahoo.com
From the MSDN AfxBeginThread description is appers that overloading
AfxBeginThread with RUNTIME_CLASS(thread) as the first parameter
creates a user interface thread. "LPVOID pParameter" is not among
parameters of an interface thread overloading.
Here is a two-step way to pass parameters to a UI thread:

pThread = (CMyThread *)AfxBeginThread(
RUNTIME_CLASS(CMyThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
pThread->m_hModalWait = CreateEvent(NULL, FALSE, FALSE, NULL);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
--
Scott McPhillips [MVP VC++]
r***@yahoo.com
2007-07-07 21:13:12 UTC
Permalink
Post by Scott McPhillips [MVP]
Scott McPhillips [MVP VC++]
Scott - many thanks, I will give it a try!
unknown
2010-04-28 09:57:15 UTC
Permalink
Hi

if two users are working simultaneously in a same form,How to refresh the data automatically which was added by the other user.

I herad that through windows API 'Postmessage' and 'registerdwindowmessgae' we can handle this.

Please give some examples



runcyclexcsk wrote:

Scott - many thanks, I will give it a try!
07-Jul-07

Scott - many thanks, I will give it a try!

Previous Posts In This Thread:

On Wednesday, July 04, 2007 4:03 PM
runcyclexcsk wrote:

passing "parent" ID to a new thread
Hi all

secondary window and to communicate with the main window b
PostMessage

pNewThread = (CNewThread*)AfxBeginThread(RUNTIME_CLASS(CNewThread),0)

I need to pass the ID of the main window to the new thread when
create the new thread, to be able to PostMessage to the main windo
later. How do I pass the main widnow ID to the new thread?
understand that there is no parent-child reglationship between threads.

On Wednesday, July 04, 2007 4:21 PM
Doug Harrison [MVP] wrote:

Re: passing "parent" ID to a new thread
On Wed, 04 Jul 2007 13:03:41 -0700, ***@yahoo.com wrote

If there were such a relationship, it wouldn't be between a window and
thread; it would be between the threads. For a number of reasons, it i
usually better for one thread to create all the windows; this thread i
called the "main UI thread" among other things. That said, to pas
information into a thread when you create it, use the second AfxBeginThrea
parameter for which you're currently passing 0. If it's pointer-sized o
smaller, you can just cast it to LPVOID when you call AfxBeginThread an
back to the original type in the thread entry function; it's common to d
this with the "this" pointer of a member function. If you need to pass
lot of information and can't do it with "this", create a new class o
struct, dynamically create an instance with new, and pass a pointer to i
as the thread parameter. The created thread then assumes ownership of th
object and must delete it when done with it. This hand-off protocol avoid
lifetime issues, and you may need to use it when you PostMessage betwee
threads. Finally, for some info on safely using AfxBeginThread, see

http://members.cox.net/doug_web/threads.ht

--
Doug Harriso
Visual C++ MVP

On Wednesday, July 04, 2007 4:30 PM
runcyclexcsk wrote:

Doug,Thanks a bunch, it hepled a lot.
Doug

Thanks a bunch, it hepled a lot.

On Wednesday, July 04, 2007 10:05 PM
Ben Voigt [C++ MVP] wrote:

Re: passing "parent" ID to a new thread
If there is only one "main window", then a global variable will wor
perfectly fine.

On Wednesday, July 04, 2007 10:50 PM
runcyclexcsk wrote:

I know, this is how I've been doing it so far, but I am drowning inglobals by
I know, this is how I have been doing it so far, but I am drowning i
globals by now... too many of them. I am trying to make it cleaner.

On Wednesday, July 04, 2007 11:13 PM
Ben Voigt [C++ MVP] wrote:

Re: passing "parent" ID to a new thread
static member variable... to keep things organize

But it still acts like a global.

On Thursday, July 05, 2007 12:27 AM
Scott Seligman [MSFT] wrote:

Re: passing "parent" ID to a new thread
Why not pass it in as the pParam of AfxBeginThread()

-
Scott Seligman [MSFT
This posting is provided AS IS with no warranties, and confer
no rights.

On Friday, July 06, 2007 12:34 AM
runcyclexcsk wrote:

Yeah, I am trying to figure it out, but I am confused about converting"this"
Yeah, I am trying to figure it out, but I am confused about convertin
"this" (which is a pointer to a CDialog class) to LPVOID. If I jus
d

AfxBeginThread(RUNTIME_CLASS(Fluoro_532_thread), this

...it does not work, and casting "this" to (void *) does not wor
either.

On Friday, July 06, 2007 12:50 AM
runcyclexcsk wrote:

AfxBeginThread with RUNTIME_CLASS(thread) as the first parametercreates a user
AfxBeginThread with RUNTIME_CLASS(thread) as the first paramete
creates a user interface thread. "LPVOID pParameter" is not amon
parameters of an interface thread overloading.

On Friday, July 06, 2007 7:33 AM
Scott McPhillips [MVP] wrote:

Re: passing "parent" ID to a new thread
***@yahoo.com wrote:

Here is a two-step way to pass parameters to a UI thread:

pThread = (CMyThread *)AfxBeginThread(
RUNTIME_CLASS(CMyThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
pThread->m_hModalWait = CreateEvent(NULL, FALSE, FALSE, NULL);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();
--
Scott McPhillips [MVP VC++]

On Saturday, July 07, 2007 5:13 PM
runcyclexcsk wrote:

Scott - many thanks, I will give it a try!
Scott - many thanks, I will give it a try!


Submitted via EggHeadCafe - Software Developer Portal of Choice
A Framework to Animate WPF and Silverlight Pages Similar to the PowerPoint Slides
http://www.eggheadcafe.com/tutorials/aspnet/7390a840-dd39-4c35-9940-c7354940d878/a-framework-to-animate-wp.aspx
Brian Muth
2010-04-28 17:04:46 UTC
Permalink
You have posted in a C++ newsgroup. Try one of the microsoft.public.vb.*
newsgroups.

Loading...