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

C / C++ / MFC

 
GeneralRe: Show Desktop Problem which minmizes all the window Pin
Waldermort30-Aug-06 20:52
Waldermort30-Aug-06 20:52 
QuestionProblem in Transparent Edit Control. Pin
uday kiran janaswamy30-Aug-06 19:23
uday kiran janaswamy30-Aug-06 19:23 
AnswerRe: Problem in Transparent Edit Control. Pin
payal33530-Aug-06 20:16
payal33530-Aug-06 20:16 
AnswerRe: Problem in Transparent Edit Control. Pin
Hamid_RT30-Aug-06 22:50
Hamid_RT30-Aug-06 22:50 
QuestionRegFlushKey doubt Pin
ashokvishnu30-Aug-06 19:14
ashokvishnu30-Aug-06 19:14 
AnswerRe: RegFlushKey doubt Pin
Waldermort30-Aug-06 20:17
Waldermort30-Aug-06 20:17 
GeneralRe: RegFlushKey doubt Pin
ashokvishnu30-Aug-06 20:42
ashokvishnu30-Aug-06 20:42 
QuestionThead questions Pin
Nicknz30-Aug-06 19:13
Nicknz30-Aug-06 19:13 
Hi,

I wanted to create a threaded dialog app however I couldn't find a simple example I could build on. I have now figured out a way to do it mainly based on this article (Unfortunaly you have to download windows speech to get it to run). So I am thinking of using it to create my first article here on code project.

My dialog consists of a start and stop button and an edit control.
The code for the start and stop buttons:


void CSimpleThreadDlg::OnBnClickedOk()<br />
{<br />
	int i = 3;<br />
	theApp.m_stop = false;<br />
	theApp.m_pThread =AfxBeginThread(simThread,(LPVOID)i);<br />
	m_start.EnableWindow(0);<br />
}<br />
<br />
void CSimpleThreadDlg::OnBnClickedStop()<br />
{<br />
	// TODO: Add your control notification handler code here<br />
	theApp.m_stop = true;<br />
	m_start.EnableWindow(1);<br />
}


And the code for my simple thread:

#include "stdafx.h"<br />
#include "SimpleThread.h"<br />
#include "thread.h"<br />
<br />
unsigned int simThread(LPVOID pParam)<br />
{<br />
	unsigned int i = (unsigned int)pParam;<br />
	CWnd* pEditControl=theApp.m_pMainWnd ->GetDlgItem (IDC_EDIT1);<br />
	CString message;<br />
	while(!theApp.m_stop)<br />
	{<br />
		pEditControl->GetWindowText(message);<br />
		message.AppendFormat(_T("\r\nHello World #%i"),i);<br />
		pEditControl->SetWindowText(message);<br />
		Sleep(500);<br />
		i++;<br />
		if(i>10)<br />
		{<br />
			i = 0;<br />
			<br />
			pEditControl->SetWindowText("");<br />
		}<br />
	}<br />
	PostQuitMessage(0);<br />
	return 0;<br />
}


As you can see I disabled the Start button until the Stop button is pressed. If I don't do that it is possible to create multiple theads (Which is a bad thing since I believe it would overwrite the pointer - still leaving the existing thread running. The multiple threads also fight for the dialog.). When the dialog is closed in this situation the debugger reports memory leaks.

Now for my real app I want to kill the thread when the dialog closes...

	CSimpleThreadDlg dlg;<br />
	m_pMainWnd = &dlg;<br />
	INT_PTR nResponse = dlg.DoModal();<br />
	if (nResponse == IDOK)<br />
	{<br />
		// TODO: Place code here to handle when the dialog is<br />
		//  dismissed with OK<br />
	}<br />
	else if (nResponse == IDCANCEL)<br />
	{<br />
		// TODO: Place code here to handle when the dialog is<br />
		//  dismissed with Cancel<br />
	}<br />
	MSG msg;<br />
	theApp.m_stop = true;//Stop my thread<br />
return FALSE;<br />
}


However this also has a memory leak, since the application closes before the second thread has a chance to execute...

So what is the correct way around this one? A simple Sleep(10); does the trick since it gives the other thread a chance to run and read m_stop.

Or I figured I could look at PeekMessage to see if the thread had sent the WM_QUIT message. However when debugging I looked at what peak message returned before setting m_stop and it equalled WM_QUIT. (I assumme it was the quit message for the dialog?) So the following loop to check if the thread has finished is not very useful.

<br />
	do{ <br />
		Sleep(10);<br />
		PeekMessage(&msg, dlg,  0, 0, PM_NOREMOVE);<br />
	}<br />
	while(msg.message != WM_QUIT);


Another question - is the thread a UI thread or a worker thread?

Thanks for your help Smile | :)
Nick
AnswerRe: Thead questions Pin
cmk1-Sep-06 0:08
cmk1-Sep-06 0:08 
GeneralRe: Thead questions Pin
Nicknz3-Sep-06 14:09
Nicknz3-Sep-06 14:09 
Questionlocale dll, sugg please Pin
spicy_kid200030-Aug-06 18:21
spicy_kid200030-Aug-06 18:21 
AnswerRe: locale dll, sugg please Pin
Waldermort30-Aug-06 20:14
Waldermort30-Aug-06 20:14 
AnswerRe: locale dll, sugg please Pin
toxcct30-Aug-06 21:48
toxcct30-Aug-06 21:48 
QuestionDIB and normal BITMAP Pin
HakunaMatada30-Aug-06 18:08
HakunaMatada30-Aug-06 18:08 
AnswerRe: DIB and normal BITMAP Pin
Joe Woodbury30-Aug-06 18:27
professionalJoe Woodbury30-Aug-06 18:27 
AnswerRe: DIB and normal BITMAP Pin
Jörgen Sigvardsson30-Aug-06 21:38
Jörgen Sigvardsson30-Aug-06 21:38 
Questioncopy/store to a file Pin
thathvamsi30-Aug-06 17:22
thathvamsi30-Aug-06 17:22 
AnswerRe: copy/store to a file Pin
Rinu_Raj30-Aug-06 17:38
Rinu_Raj30-Aug-06 17:38 
GeneralRe: copy/store to a file Pin
thathvamsi30-Aug-06 18:01
thathvamsi30-Aug-06 18:01 
GeneralRe: copy/store to a file Pin
Rinu_Raj30-Aug-06 18:10
Rinu_Raj30-Aug-06 18:10 
GeneralRe: copy/store to a file Pin
thathvamsi30-Aug-06 18:29
thathvamsi30-Aug-06 18:29 
GeneralRe: copy/store to a file Pin
Rinu_Raj30-Aug-06 19:09
Rinu_Raj30-Aug-06 19:09 
GeneralRe: copy/store to a file Pin
thathvamsi30-Aug-06 20:05
thathvamsi30-Aug-06 20:05 
GeneralRe: copy/store to a file Pin
kakan30-Aug-06 20:25
professionalkakan30-Aug-06 20:25 
GeneralRe: copy/store to a file Pin
thathvamsi30-Aug-06 20:37
thathvamsi30-Aug-06 20:37 

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.