Re: Button Control Event: question
- From: "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
- Date: Mon, 04 Jul 2005 10:00:58 -0500
Marcus wrote:
Thanks everyone for your input. It's helped quite a bit, and I've learned more than I thought I would need to... I thought this might have a much simpler solution! :-)
I've been working through Joseph's tutorials (what a great resource!!) and actually managed to get through some of them. I was able to create a seperate thread that processes the loop and now the test message print's fine and doesn't crash the app :-). The only problem is that I'm unable to get it to stop. Here is the code below:
UINT CTestAPIDlg::run(LPVOID p) { CTestAPIDlg * me = (CTestAPIDlg *)p;
me->run();
return 0; } void CTestAPIDlg::run() { int on = GetDlgItemInt( IDC_EDIT1, FALSE ); while(on == 1) { m_list.InsertString(0,"loop"); Sleep(1000); }
running = FALSE; } void CTestAPIDlg::OnStart() { running = TRUE; myWorkerThread = AfxBeginThread(run, this); } void CTestAPIDlg::OnTurnOff() { running = FALSE; // I intially tried just this // statement, to no avail WaitForSingleObject(myWorkerThread->m_hThread, INFINITE); delete myWorkerThread; CTestAPIDlg::OnTurnOff(); // I also tried leaving this // out as I wasn't sure I understood it's purpose. }
Currently, the GUI freezes after the TurnOff function is executed. I understand how I was able to start it, which makes sense, but the deactivation end of thread handling is baffling me at the moment. I'm not sure if this has to do with my loop or with my misunderstanding of how to correctly terminate a thread such as this one. I'm looking over some other tutorials at the moment, so hopefully I can find a solution. Anyone with more thread handling experience than I, who doesn't mind giving a hand, would be most welcome in the venturing of some suggestions.
Thanks very much for the help so far.
Marcus
You need code in the thread's while loop to detect the termination request. As it is written now it does not check the 'running' variable.
What is m_list? If it is a control then you have a possible deadlock condition when stopping the thread. Accessing a control from a worker thread will wait until the main thread processes the message to the control. But if the main thread is in WaitFor... it will never process the message, so both threads lock up. Do not access controls from a thread that did not create them.
-- Scott McPhillips [VC++ MVP]
.
- Follow-Ups:
- Re: Button Control Event: question
- From: Marcus
- Re: Button Control Event: question
- References:
- Button Control Event: question
- From: Marcus
- Re: Button Control Event: question
- From: Josh McFarlane
- Re: Button Control Event: question
- From: Joseph M . Newcomer
- Re: Button Control Event: question
- From: Marcus
- Button Control Event: question
- Prev by Date: Re: Can I prevent the Setup Solution from building Every time?
- Next by Date: Re: How to test memory leak in OCX
- Previous by thread: Re: Button Control Event: question
- Next by thread: Re: Button Control Event: question
- Index(es):
Relevant Pages
|