Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't write to static edit box from a different thread

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
US
I want to start a thread then write back to the main dialog to show the progress. It gets lost on the SetWindowText() function. Don't know where it goes. Here is the code:

This is in the header file:
Code:
static unsigned int __stdcall TestProgressTimer(void* arglist);

This is the button that starts the other thread:
Code:
void CAnvilDlg::OnBnClickedStart()
{
   HANDLE hTestTimerThread;
   unsigned int threadID;

   // Start the test timer thread.
   hTestTimerThread = (HANDLE)_beginthreadex(NULL, 0, TestProgressTimer,(void*) this, 0, &threadID);
   WaitForSingleObject(hTestTimerThread, INFINITE);
   CWnd* hEditBox = (CRichEditCtrl*)GetDlgItem(IDC_ACTIVITY);
   hEditBox->SetWindowTextA("Timer Returned");
}

This is the function call in _beginthreadex();
Code:
unsigned int __stdcall CAnvilDlg::TestProgressTimer(void* arglist)
{
   unsigned int counter = 0;
   unsigned long lstart;
   CAnvilDlg* MainDlg = (CAnvilDlg*) arglist;
   CWnd* hTimeLabel;
   char strCount[32];

   lstart = GetTickCount();
   while (counter < 10)
   {
      if (GetTickCount() > (lstart + 1000))
      {
         counter++;
         sprintf_s(strCount, sizeof(strCount), "%d", counter);
         hTimeLabel = MainDlg->GetDlgItem(IDC_ACTIVITY);
         hTimeLabel->SetWindowTextA(strCount);
         lstart = GetTickCount();
      }
   }
	_endthreadex(0);
	return 0;
}

I single step through this code and it goes off into forever land when it tries to execute the SetWindowTextA() function.

It looks like it has good handle values but it won't come back from that call.

What am I doing wrong?

thanks,

HyperEngineer
If it ain't broke, it probably needs improvement.
 
You also need to declare your thread function as 'static' to suppress the hidden 'this' pointer from being passed around (or expected).

Code:
static unsigned int __stdcall CAnvilDlg::TestProgressTimer(void* arglist)
Ditto in the class.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 

I tried this and it says that I can't use the static keyword on a member function defined at file scope (error C2724).

Thanks,

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top