Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC Threading Message
Hi,
I am new to MFC, I am using a thread and want to send a message to dialogue class using that thread(ThreadProc) so i can call a function in the dialogue class using that thread. Please tell me the message code to use.(Sendmessage(?????????????))
 
Think i need to send "ON_CBN_SELCHANGE" and id is "IDC_PType" (Please read the code)
Tanks
Here is the code
 
// UMKurunegalaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UMKurunegala.h"
#include "UMKurunegalaDlg.h"
#include "afxdialogex.h"

 

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static int Data_Of_Thread_1 = 1;
static HANDLE Handle_Of_Thread = 0;
int val = 0;
 
void ThreadProc(void *param)// *** The thread i am using
{
    int h=*((int*)param);
 
	SendMessage(?????????????????????????????????????????????????????);
    
    _endthread();
}
		/*DWORD WINAPI setBitThread(LPVOID lpParam) 
{	
	CUMKurunegalaDlg::OnCbnEditchangePtype();
    return 0; 
} */
 
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();
 
// Dialog Data
	enum { IDD = IDD_ABOUTBOX };
 
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};
 
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
 

// CUMKurunegalaDlg dialog

 

 
CUMKurunegalaDlg::CUMKurunegalaDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CUMKurunegalaDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
 
void CUMKurunegalaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, teststt, testt);
	DDX_Control(pDX, IDC_VType, vtypec);
	DDX_Control(pDX, IDC_pricest, pricest);
	DDX_Control(pDX, IDC_VBrand, vbrandc);
	DDX_Control(pDX, IDC_VModel, vmodelc);
	DDX_Control(pDX, IDC_VPart, vpartc);
	DDX_Control(pDX, IDC_PType, ptypec);
 
}
 
BEGIN_MESSAGE_MAP(CUMKurunegalaDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_VType, &CUMKurunegalaDlg::OnCbnSelchangeVtype)
	ON_CBN_SELCHANGE(IDC_VBrand, &CUMKurunegalaDlg::OnCbnSelchangeVbrand)
	ON_CBN_SELCHANGE(IDC_VModel, &CUMKurunegalaDlg::OnCbnSelchangeVmodel)
	ON_CBN_SELCHANGE(IDC_VPart, &CUMKurunegalaDlg::OnCbnSelchangeVpart)
	ON_CBN_SELCHANGE(IDC_PType, &CUMKurunegalaDlg::OnCbnSelchangePtype)
	ON_CBN_EDITCHANGE(IDC_PType, &CUMKurunegalaDlg::OnCbnEditchangePtype)
	ON_CBN_CLOSEUP(IDC_PType, &CUMKurunegalaDlg::OnCbnCloseupPtype)
END_MESSAGE_MAP()
Posted 18-Aug-12 0:29am
Comments
pasztorpisti at 18-Aug-12 6:54am
   
The simple answer to your question: You can not send a message to a class.
Please read through a beginner C++ tutorial before jumping on advanced topics like threading and gui because otherwise you wont be able to ask the right questions and you will be unable to understand our answers.
PS: Please don't double post a question!
stib_markc at 18-Aug-12 7:23am
   
Pardon me if I am wrong but I think what he meant was, to send a message to the dialogue from a thread.
pasztorpisti at 18-Aug-12 7:31am
   
Yes I know. But if he knows what the difference is between a class and an instance then he should post some other part of the code. You simply can not answer his question if he isn't in knowledge of basic terms and he wants to put together something using 2 relatively difficult things: threading and gui. The result is something full of bugs if it works at all.
I don't think you want to start teaching him how to code in C++. Anyway we answered his question in another post but he couldnt understand it.
wedagedara at 18-Aug-12 8:04am
   
HI,
I have solved the problem using this in the thread
AfxGetApp()->m_pMainWnd->PostMessage(WM_APP+2,0,0);
and using this in the message map of dialogue class
ON_MESSAGE(WM_APP+2, &CUMKurunegalaDlg::testfunction)
 
I just wanted to call a member function of "UMKurunegalaDlg" using a thread
 
Thanks for trying to helping me. (I don't want to learn c++ or time to learn it, i just want to complete my degree project, that's the reason for stupid questions that am asking)
Good day
pasztorpisti at 18-Aug-12 8:43am
   
Thats OK, PostMessage should do the job well. Still don't understand why is it good for you or some other teachers if you are doing something you dont really understand. But this goes beyond this topic and your actual task, its more about the way universities work...
Sergey Alexandrovich Kryukov at 18-Aug-12 18:07pm
   
You are right, of course.
My first reaction was: "come on, this is not quite Smalltalk" :-)
--SA
pasztorpisti at 18-Aug-12 18:26pm
   
LOL, thats hilarious! :-) You made my day! :-)
Sergey Alexandrovich Kryukov at 18-Aug-12 22:19pm
   
:-)
Richard MacCutchan at 18-Aug-12 7:00am
   
I second pasztorpisti's remarks. I would also say that you are using a dialog in the wrong way. Dialog's are popup windows used to get information from the user, not to pass information between threads. A dialog already has built in methods to get or set the data that it is responsible for.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I suggest to use PostThreadMessage instead of SendMessage, check PostThreadMessage Demystified[^] for reasons. For Documentation see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644946%28v=vs.85%29.aspx[^]
 
And by the way if you want to invoke a function of your dialog class from your thread, then you need to send the "this"(present dialog object reference) as a parameter to the thread procedure. Are you sure you are doing this? Because in the code you are casting you lParam into an int which baffles me. When you call your threadproc you need to send "this" as "lParam".
With SendMessage the code would look something like :
 void ThreadProc(LPVOID lParam)
{
   CCurrentDialog* pDlg = (CCurrentDialog*)lParam;
   SendMessage(pDlg->m_hWnd,ON_CBN_SELCHANGE,NULL,NULL);
}
 
You can use PostMessage[^] also.
  Permalink  
v2
Comments
wedagedara at 18-Aug-12 7:55am
   
Your answer was helped, thanks
stib_markc at 21-Aug-12 0:20am
   
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 325
1 OriginalGriff 278
2 Nguyen.H.H.Dang 230
3 Peter Leow 195
4 arvind mepani 187


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 18 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid