|
Prev: Writing a .txt file
Next: Standard C++ Library
From: keith on 1 Apr 2008 13:42 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. Do you have any way to pass parameters? Thanks
From: Mark Salsbery [MVP] on 1 Apr 2008 13:53 "keith" <keith(a)discussions.microsoft.com> wrote in message news:825D91E6-87F8-40CA-80D2-2D84330ADBA3(a)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 >
From: Igor Tandetnik on 1 Apr 2008 13:56 keith <keith(a)discussions.microsoft.com> wrote: > 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. Well, you managed to pass an HWND from main thread to worker. If you need more parameters, just define a struct with a field for each parameter, set up those fields, then pass the struct's pointer to AfxBeginThread. In the other direction, note the last two parameters of PostMessage. You can pass arbitrary values there (e.g. again a pointer to the struct containing additional information). Or, you can simply have extra fields in the structure you passed to AfxBeginThread, and have the worker thread fill them in. When the thread finishes, the main thread can read the values back from the structure (make sure it lives long enough). -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: keith on 1 Apr 2008 14:16 Since I need to use user defined message, I have to start a thread by AfxBeginThread(ThreadProc, GetSafeHwnd()); cannot use AObject *p=new AObject); AfxBeginThread(ThreadProc, (LPVOID)&p); How to work with it? "Mark Salsbery [MVP]" wrote: > "keith" <keith(a)discussions.microsoft.com> wrote in message > news:825D91E6-87F8-40CA-80D2-2D84330ADBA3(a)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 > >
From: David Wilkinson on 1 Apr 2008 14:50
keith wrote: > Since I need to use user defined message, I have to start a thread by > AfxBeginThread(ThreadProc, GetSafeHwnd()); > > cannot use > AObject *p=new AObject); > AfxBeginThread(ThreadProc, (LPVOID)&p); > > How to work with it? Keith: Do like this AfxBeginThread(ThreadProc, this); In the the thread function, cast the LPVOID parameter back to a pointer to your CWnd class. Now you have all the public members of your CWnd class available in the thread. In particular m_hWnd is the HWND. Just be careful not to call any MFC functions on the CWnd pointer. -- David Wilkinson Visual C++ MVP |