Solved

a question about UI Thread

Posted on 2002-07-19
3
416 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now