Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

a question about UI Thread

Posted on 2002-07-19
3
Medium Priority
?
454 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
arul_m earned 300 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

Independent Software Vendors: 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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

916 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