Solved

a question about UI Thread

Posted on 2002-07-19
3
421 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

15 Experts available now in Live!

Get 1:1 Help Now