Click here to Skip to main content
14,642,321 members
Rate this:
Please Sign up or sign in to vote.
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)
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
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);

		/*DWORD WINAPI setBitThread(LPVOID lpParam) 
    return 0; 
} */

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)

void CAboutDlg::DoDataExchange(CDataExchange* pDX)


// CUMKurunegalaDlg dialog

CUMKurunegalaDlg::CUMKurunegalaDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CUMKurunegalaDlg::IDD, pParent)
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

void CUMKurunegalaDlg::DoDataExchange(CDataExchange* 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_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)
Updated 11-Jul-20 19:52pm
xenotron 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.
xenotron 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
I have solved the problem using this in the thread
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
xenotron 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 18-Aug-12 18:07pm
You are right, of course.
My first reaction was: "come on, this is not quite Smalltalk" :-)
xenotron 18-Aug-12 18:26pm
LOL, thats hilarious! :-) You made my day! :-)
Sergey Alexandrovich Kryukov 18-Aug-12 22:19pm
Richard MacCutchan 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:
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[^]

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;

You can use PostMessage[^] also.
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100