Re: Button Control Event: question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]

.



Relevant Pages

  • Re: Asp.Net AJAX dynamically added Tab Control not displaying
    ... TabPanel assumes there's no active tab and it hides all the tabs at client ... protected void Page_PreRender ... Microsoft is providing this information as a convenience to you. ... does not control these sites and has not tested any software or information ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: How to transparent editbox control works correctly...
    ... can't simply attach a CString to your edit control and hope that UpdateData ... void SetFont; ... afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); ...
    (microsoft.public.vc.mfc)
  • Need help getting HWND for SendMessage() in MFC ActiveX user control
    ... I am writing an MFC ActiveX control for Windows CE. ... virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& ... virtual DWORD GetControlFlags; ... virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Batch file and MFC (Properly Terminating Application)
    ... // CDummy1Dlg message handlers ... // Set the icon for this dialog. ... void CDummy1Dlg::OnPaint ... Use a control variable and do ...
    (microsoft.public.vc.mfc)
  • TextBox Readonly OnPaint wrong font! Please help!
    ... I have a read-only textBox which shows the results of a selection on ... This works until I left click the control, ... protected override void OnPaint ... private txtBx txtBxWithFontProblem; ...
    (microsoft.public.dotnet.languages.csharp)