Re: pass parameter between main thread and working thread
"keith" <keith@discussions.microsoft.com> wrote in message
news:A7D3CFAB-FB43-4243-A157-201E25E7CAAD@microsoft.com...
Since I need to use user defined message, I have to start a thread by
AfxBeginThread(ThreadProc, GetSafeHwnd());
The message you send has nothing to do with the parameter you pass to the
thread proc, except for the fact you need a window to send a message to.
What I meant was something like this:
//-------------------------------------------------------------
UINT AFX_CDECL ThreadProc(LPVOID pParam)
{
CMyWnd *pMyWnd = (CMyWnd *)pParam;
//... do stuff in the thread using pMyWnd to access other parameters if
necessary ...
pMyWnd->PostMessage(WM_THREADFINISHED, 0, 0);
return 0;
}
....
AfxBeginThread(ThreadProc, this);
//-------------------------------------------------------------
You could also use Igor's suggestion, and pass a structure with all the
parameters you need:
//-------------------------------------------------------------
struct MyThreadParams
{
HWND TheWnd;
int someintparameter;
};
UINT AFX_CDECL ThreadProc(LPVOID pParam)
{
MyThreadParams *pThreadParams = (MyThreadParams *)pParam;
...
::PostMessage(pThreadParams->TheWnd, WM_THREADFINISHED, 0, 0);
return 0;
}
....
MyThreadParams *pThreadParams = new MyThreadParams; // the
WM_THREADFINISHED handler should delete this object!!
pThreadParams->TheWnd = GetSafeHwnd();
pThreadParams->someintparameter = 42;
AfxBeginThread(ThreadProc, pThreadParams);
//-------------------------------------------------------------
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
cannot use
AObject *p=new AObject);
AfxBeginThread(ThreadProc, (LPVOID)&p);
How to work with it?
"Mark Salsbery [MVP]" wrote:
"keith" <keith@discussions.microsoft.com> wrote in message
news:825D91E6-87F8-40CA-80D2-2D84330ADBA3@microsoft.com...
I started a working thread by
AfxBeginThread(ThreadProc, GetSafeHwnd());
In the ThreadProc function, I sent a user message
::PostMessage((HWND)pParam, WM_THREADFINISHED, 0, 0)
when it is done and a main thread function handles the message.
note: WM_THREADFINISHED is user defined message.
The problem is how to pass parameter from main thread to the working
thread
and vice versa.
I can do it to pass parameter to working thread by
CString c("abc");
AfxBeginThread(ThreadProc,(LPVOID)&c);
But main thread function cannot receive the user message.
You only have one pointer-sized parameter you can pass to the thread
proc,
so you have two choices...
1) Pass a pointer to an object that has all the info the thread needs
(recommended)
2) Use global variable(s) - all threads have access to those (not
recommended)
In this case, since the thread is created from the window that is to
receive
the finished message, I would pass a pointer to the window object, and in
the window class, provide whatever methods are needed for the thread to
get
the required parameters from the window class through the passed window
object pointer.
Hopefully that made sense :)
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
Do you have any way to pass parameters?
Thanks