Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

C++
// 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
Updated 11-Jul-20 19:52pm
Comments
pasztorpisti 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 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 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 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 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...

1 solution

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.
 
Share this answer
 
v2
Comments
wedagedara 18-Aug-12 7:55am    
Your answer was helped, thanks
stib_markc 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900