Solved

a question about UI Thread

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

617 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