Click here to Skip to main content
15,890,717 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionproblem with device context (and some other bad things) Pin
csrss4-Mar-11 8:38
csrss4-Mar-11 8:38 
QuestionRe: problem with device context (and some other bad things) Pin
CPallini4-Mar-11 9:50
mveCPallini4-Mar-11 9:50 
AnswerRe: problem with device context (and some other bad things) Pin
csrss4-Mar-11 9:56
csrss4-Mar-11 9:56 
AnswerRe: problem with device context (and some other bad things) Pin
Ozer Karaagac4-Mar-11 14:17
professionalOzer Karaagac4-Mar-11 14:17 
GeneralRe: problem with device context (and some other bad things) Pin
csrss4-Mar-11 15:40
csrss4-Mar-11 15:40 
AnswerRe: problem with device context (and some other bad things) Pin
Richard MacCutchan4-Mar-11 22:17
mveRichard MacCutchan4-Mar-11 22:17 
GeneralRe: problem with device context (and some other bad things) Pin
csrss5-Mar-11 0:53
csrss5-Mar-11 0:53 
QuestionSending windows message from worker thread Pin
GC1044-Mar-11 0:41
GC1044-Mar-11 0:41 
Hello,

I am currently getting to grips with MFC, VC++, VS2010 and threading. In particular I am trying to start a thread from a CMainFrame object, then post a windows message from the thread back to he CMainFrame object such that a messagebox can be displayed indicating the progress of the thread.

In this example the uses starts the thread from a drop-down menu item (CreateWorkerThread function is called) the thread code executes and sends a single windows message towards the end of the thread. The problem I have is that CMainFrame doesn't seem to respond to the message unless the function that starts the thread uses a ::WaitForSingleObject() call. Why do I need to effectively pause the execution of the function creating the thread?

Mayby naively, I assumed the following actions:

-User selects Menu item
-CreateWorkerThread() is called
-Thread executes in parallel with the remains of CreateWorkerThread()
-Worker thread generates a windows message (using ::PostMessage())
-By now CreateWorkerThread() would have completed.
-CMainFrame Message map processes Worker Thread' message 'WM_THREAD1MSG'
-Messagebox created and presented to user.


void CMainFrame::CreateWorkerThread(void)
// Created 25FEB2011
{


	HWND hWnd = GetSafeHwnd();
	if (hWnd == NULL)
	{
		TRACE0("Window pointer is invalid!");
	}
	

	hWnd = GetSafeHwnd();	//hWnd has been made a 'public' member

	pThread1 = AfxBeginThread(mcProc, static_cast<LPVOID>(&hWnd), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);	
	pThread1 ->ResumeThread(); 

	DWORD ThreadExitCodeValue = 0;
	LPDWORD pDWord = NULL;
	pDWord = &ThreadExitCodeValue;

	//Need a handle for the thread:
	HANDLE pThreadHandle = pThread1->m_hThread;


	::WaitForSingleObject(pThreadHandle,INFINITE); //stops this 'thread' until worker thread exits.


}


BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
	ON_WM_CREATE()

	ON_MESSAGE(WM_THREAD1MSG, Respond2ThreadMsg)  //#define WM_THREAD1MSG WM_APP + 0x10 <- this line included in stdafx.h

	ON_COMMAND(ID_VIEW_CUSTOMIZE, &CMainFrame::OnViewCustomize)
	ON_REGISTERED_MESSAGE(AFX_WM_CREATETOOLBAR, &CMainFrame::OnToolbarCreateNew)
	ON_COMMAND_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnApplicationLook)
	ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnUpdateApplicationLook)

	ON_COMMAND(ID_GCCODEDVMT_TESTCOMMAND, TestCommand)		//GC added 23FEB2011
	ON_COMMAND(ID_GCCODEDVMT_CREATEWORKERTHREAD, CreateWorkerThread)	//GC Added 25FEB2011
	ON_COMMAND(ID_GCCODEDVMT_TERMINATEWORKERTHREAD, TerminateWorkerThread)	//GC added 28FEB2011
	


	ON_WM_SETTINGCHANGE()
END_MESSAGE_MAP()


UINT mcProc(LPVOID param)
// Last edited 2MAR2011
{
	//This is the worker thread function...
	CString TempString;
	CString TempString2;
	int i = 0;
	int y = 0;

	HWND *pHandle = static_cast<HWND*>(param);	//for benefit of sending windows message from thread
	//static_cast works in the same way asordianry casting but considered to be restrictive(safer) and 'noticeable'...


	for (i=0; i<10; i++)
	{
		TempString.Format(_T("index counter: %d, \n"), i); 
		TempString2 = _T("Thread 1 Started... ");
		TempString2 += TempString;

		TRACE(TempString2);
		::Sleep(1000);
		
	}


	
	
	//send message from this thread...

	y = ::PostMessage(*pHandle, WM_THREAD1MSG, 0, 0);
	

	Sleep(1000);
	
	return 0;
}


LRESULT CMainFrame::Respond2ThreadMsg(WPARAM xParam, LPARAM lParam)
// Created 25FEB2011
{
	AfxMessageBox(_T("Thread message received"));
	TRACE(_T("Thread message received /n"));
	return 0;
}


Opinions greatfully received.

regards

Geoff
AnswerRe: Sending windows message from worker thread Pin
CPallini4-Mar-11 1:47
mveCPallini4-Mar-11 1:47 
GeneralRe: Sending windows message from worker thread Pin
GC1044-Mar-11 2:29
GC1044-Mar-11 2:29 
GeneralRe: Sending windows message from worker thread Pin
prasad_som4-Mar-11 2:33
prasad_som4-Mar-11 2:33 
GeneralRe: Sending windows message from worker thread Pin
CPallini4-Mar-11 3:05
mveCPallini4-Mar-11 3:05 
GeneralRe: Sending windows message from worker thread Pin
CPallini4-Mar-11 2:36
mveCPallini4-Mar-11 2:36 
AnswerRe: Sending windows message from worker thread Pin
prasad_som4-Mar-11 2:30
prasad_som4-Mar-11 2:30 
QuestionGetting Time Difference [modified] Pin
AmbiguousName3-Mar-11 22:29
AmbiguousName3-Mar-11 22:29 
AnswerRe: Getting Time Difference Pin
CPallini3-Mar-11 22:46
mveCPallini3-Mar-11 22:46 
AnswerRe: Getting Time Difference Pin
Cool_Dev3-Mar-11 23:40
Cool_Dev3-Mar-11 23:40 
AnswerRe: Getting Time Difference Pin
David Crow4-Mar-11 2:07
David Crow4-Mar-11 2:07 
AnswerRe: Getting Time Difference Pin
Andrew Brock4-Mar-11 20:06
Andrew Brock4-Mar-11 20:06 
Questionon button stop control transfers to a option button click code Pin
VCProgrammer3-Mar-11 20:14
VCProgrammer3-Mar-11 20:14 
QuestionRe: on button stop control transfers to a option button click code Pin
David Crow4-Mar-11 2:08
David Crow4-Mar-11 2:08 
AnswerRe: on button stop control transfers to a option button click code Pin
VCProgrammer4-Mar-11 18:09
VCProgrammer4-Mar-11 18:09 
AnswerRe: on button stop control transfers to a option button click code Pin
prasad_som4-Mar-11 2:36
prasad_som4-Mar-11 2:36 
QuestionSOLVED Application failed to initialize ?? [modified] Pin
Vaclav_3-Mar-11 13:48
Vaclav_3-Mar-11 13:48 
QuestionAdding a static library build Pin
softwaremonkey3-Mar-11 11:39
softwaremonkey3-Mar-11 11:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.