Solved

a question about UI Thread

Posted on 2002-07-19
3
437 Views
Last Modified: 2013-11-20
I have tree UI Thread in a process, the thread is based on CWinThread,and they have their own dialog. the dialog transact their own message,for example WM_TIMER Event.
But the thread can't echo my sended message.

I want to send Message from one UI thread to other UI thread, how can i do it?

this is my code,
IMPLEMENT_DYNCREATE(CDeEncryptThread, CWinThread)

CDeEncryptThread::CDeEncryptThread()
{
}

CDeEncryptThread::~CDeEncryptThread()
{
}

BOOL CDeEncryptThread::InitInstance()
{
     // TODO:  perform and per-thread initialization here
     CDeEncryptDlg *pDlg=new CDeEncryptDlg;
     pDlg->m_hWnd = m_hWnd;
     pDlg->Create(IDD_DEENCRYPTDLG);
    pDlg->ShowWindow(SW_SHOW);

     m_pMainWnd = pDlg;
     return TRUE;
}

int CDeEncryptThread::ExitInstance()
{
     // TODO:  perform any per-thread cleanup here
     return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CDeEncryptThread, CWinThread)
     //{{AFX_MSG_MAP(CDeEncryptThread)
     ON_WM_COPYDATA()
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeEncryptThread message handlers

BOOL CDeEncryptThread::OnCopyData(WPARAM wParam,LPARAM lParam)
{
     // TODO: Add your message handler code here and/or call default
     int iDataLength;
     PCOPYDATASTRUCT pcds=(PCOPYDATASTRUCT)lParam;
     char * pbuffer=(char*)malloc(pcds->cbData);
     ASSERT(NULL!=pbuffer);
     memcpy(pbuffer,pcds->lpData,pcds->cbData);
     iDataLength=pcds->cbData;
     ReplyMessage(0L);
    //AddDeEncryptData(pbuffer, iDataLength);
     return TRUE;
     //return CWinThread::OnCopyData(pWnd, pCopyDataStruct);
}

//////////in other UI thread,

     PostThreadMessage(ThreadID,WM_COPYDATA,0,(LPARAM)pCopyData);

it doesn't work.

0
Comment
Question by:fengyun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
arul_m earned 100 total points
ID: 7164370
Hi Fengyun,

Yes. It won't work. WM_COPYDATA is meant for passing message between processes (i.e Windows).

Instead try this:

Pass user defined (WM_APP+1) message to the thread using PostThreadMessage() method.
Next, implement PreTranslateMessage(MSG) method in your "CDeEncryptThread" class. Handle the WM_APP+1 message in this method. This will surely work.

Let me know if you still have problems

All the best,
Arul
0
 

Author Comment

by:fengyun
ID: 7168497
hi arul_m

the first question is that,
why do you use WM_APP+1,not use WM_USER+1?

the second is,how can i let the sending message thread waiting for my thread,like SendMessage.you know PostMessage and PostMessage functions send message and quickly return, they don't wait for a return message.

do you know what i mean?

thanks arul_m
0
 
LVL 1

Expert Comment

by:arul_m
ID: 7179396
1. WM_USER+n messages are used to pass messages within a window for its private purposes. It cannot be used across windows or throughout the application.
WM_APP+n messages are used to pass private messages between windows and threads.

2. Create an event object and share it between both the threads. In the waiting thread call WaitForSingleObject(hEvent). In the called thread, call SetEvent(hEvent) where hEvent is the handle to the event shared by both the threads.

Hope this helps.

Arul
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 100
sameEnds challenge 3 196
tripleUp challenge 7 97
unix example issues 18 114
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question