For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > sending message from UI thread to the main thread









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author sending message from UI thread to the main thread
revyakin@yahoo.com

2005-06-01, 4:00 am

I have a main control in which I have a loop which processes video
frames. To be able to see the results in real time, I created a dialog
in another thread which displays the data as they become available (the
main thread populates a global array, and the second thread picks the
last element and displays it.)

I need to access some functions (e.g. change settings of video
aquisition) in the main thread from within the second thread while the
loop in the main thread is running. I know I have to use messages to do
that, but SendMessage needs a pointer to the main window, and I can't
pass a CWnd * to the secondary thread.

So, how do I call a main thread function from a secondary thread?
Here's a sketch of what I have now:

//in main control:
pThread = (CUIThread*)AfxBeginThread(RUNTIME_CLASS
(CUIThread));
pThread->m_pMainWnd = FromHandle(this->m_hWnd);//this does not work
//in UIThread class:
CUIThread::InitInstance()
{
CWnd * cwnd = m_pMainWnd;
m_plot = new Plot_dialog(cwnd);
return TRUE;
}
//in Plot_dialog:
Plot_dialog::Plot_dialog(CWnd* pParent) {}
Plot_dialog::UpdateSetting() {
pParent->SendMessage(WM_PLOT_TO_MAIN, 0, (LPARAM) &CCM);
}

Scott McPhillips [MVP]

2005-06-01, 4:03 pm

revyakin@yahoo.com wrote:

> I have a main control in which I have a loop which processes video
> frames. To be able to see the results in real time, I created a dialog
> in another thread which displays the data as they become available (the
> main thread populates a global array, and the second thread picks the
> last element and displays it.)
>
> I need to access some functions (e.g. change settings of video
> aquisition) in the main thread from within the second thread while the
> loop in the main thread is running. I know I have to use messages to do
> that, but SendMessage needs a pointer to the main window, and I can't
> pass a CWnd * to the secondary thread.
>
> So, how do I call a main thread function from a secondary thread?
> Here's a sketch of what I have now:
>
> //in main control:
> pThread = (CUIThread*)AfxBeginThread(RUNTIME_CLASS
(CUIThread));
> pThread->m_pMainWnd = FromHandle(this->m_hWnd);//this does not work
> //in UIThread class:
> CUIThread::InitInstance()
> {
> CWnd * cwnd = m_pMainWnd;
> m_plot = new Plot_dialog(cwnd);
> return TRUE;
> }
> //in Plot_dialog:
> Plot_dialog::Plot_dialog(CWnd* pParent) {}
> Plot_dialog::UpdateSetting() {
> pParent->SendMessage(WM_PLOT_TO_MAIN, 0, (LPARAM) &CCM);
> }
>


The moment that the thread starts executing is unpredictible, so the
thread's InitInstance may have finished executing by the time you set
m_pMainWnd. This problem is easily solved. Use the CREATE_SUSPENDED
flag in AfxBeginThread, set the variable, then call pThread->ResumeThread().

You *can* pass a CWnd* to the thread. But... the thread can only use it
to access your own member variables. Attempting to access a control or
any MFC CWnd function from out-of-thread is likely to assert or deadlock.

--
Scott McPhillips [VC++ MVP]

Tim Roberts

2005-06-01, 8:59 pm

revyakin@yahoo.com wrote:

>I have a main control in which I have a loop which processes video
>frames. To be able to see the results in real time, I created a dialog
>in another thread which displays the data as they become available (the
>main thread populates a global array, and the second thread picks the
>last element and displays it.)
>
>I need to access some functions (e.g. change settings of video
>aquisition) in the main thread from within the second thread while the
>loop in the main thread is running. I know I have to use messages to do
>that,...


Not at all. All of the functions in your code are available to every
thread at any time. The only reason you might need to use SendMessage is
if you need to synchronize access to certain data structures.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com