void CTestThreadDlg::OnBnClickedButtonStart()
{
// TODO: 在此添加控件通知处理程序代码
if(!m_bStart)
{
m_bStart = TRUE;
SetTimer(0, 100, NULL);
m_ButtonStart.SetWindowTextW(_T("Stop"));
g_start = TRUE;
g_hThread = CreateThread(NULL, 0, WorkThread, m_hWnd, 0, &g_tid);
if(g_hThread == NULL)
{
MessageBox(NULL, _T("Thread creation failed!"), MB_OK|MB_ICONERROR);
}else
{
TRACE(_T("Thread [%08X] begin\r\n"), g_tid);
}
}
else
{
KillTimer(0);
m_bStart = FALSE;
m_ButtonStart.SetWindowTextW(_T("Start"));
if(g_hThread)
{
g_start = FALSE;
DWORD ret = WaitForSingleObject(g_hThread, 5000);
if (ret == WAIT_TIMEOUT)
{
MessageBox(NULL, _T("Timed out waiting for work thread to end."), MB_ICONERROR | MB_OK);
TerminateThread(g_hThread, -1);
CloseHandle(g_hThread);
g_hThread = NULL;
}
else
{
TRACE(_T("Thread [%08X] end\r\n"), g_tid);
CloseHandle(g_hThread);
g_hThread = NULL;
}
}
}
}
void CTestThreadDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
//if(0)
if(m_bStart)
if(nIDEvent==0)//my-timer
{
TRACE(_T("OnTimer: locking\r\n"));
CSingleLock lock(&g_mutex);
UINT loop = 0;
while(!lock.Lock(50))
{
MSG msg;
GetMessage(&msg, NULL, 0, 0);
//TranslateMessage(&msg);
//DispatchMessage(&msg);
//Sleep(10);
if(++loop >= 100)
{
break;
}
}
//lock.Lock();
if(lock.IsLocked())
{
TRACE(_T("OnTimer: locked\r\n"));
g_tick++;
//m_ListBox.AddString(_T("OnTimer Locked"));
//m_ListBox.SetCurSel(g_tick-1);
//Sleep(300);
CString s;
s.Format(_T("OnTimer #%d"), g_tick);
m_ListBox.AddString(s);
m_ListBox.SetCurSel(g_tick-1);
s.Format(_T("OnTimer #%d\r\n"), g_tick-1);
TRACE(_T("OnTimer: un-locking\r\n"));
lock.Unlock();
TRACE(_T("OnTimer: un-locked\r\n"));
}
else
{
TRACE(_T("OnTimer: lock failed\r\n"));
}
//TRACE(s);
}
CDialog::OnTimer(nIDEvent);
}
void CTestThreadDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: 在此处添加消息处理程序代码
//if(m_bStart)
{
m_bStart = FALSE;
KillTimer(0);
}
m_ButtonStart.SetWindowTextW(_T("Start"));
if(g_hThread)
{
g_start = FALSE;
DWORD ret = WaitForSingleObject(g_hThread, 5000);
if (ret == WAIT_TIMEOUT)
{
MessageBox(NULL, _T("Timed out waiting for work thread to end."), MB_ICONERROR | MB_OK);
TerminateThread(g_hThread, 0xff);
CloseHandle(g_hThread);
g_hThread = NULL;
}
else
{
TRACE(_T("Thread [%08X] end\r\n"), g_tid);
CloseHandle(g_hThread);
g_hThread = NULL;
}
}
}
DWORD _stdcall WorkThread(void *param)
{
UINT tick = 0;
CString s;
HWND hwnd_parent = (HWND)param;
while(g_start)
{
TRACE(_T("Thread: locking\r\n"));
CSingleLock lock(&g_mutex);
lock.Lock();
if(lock.IsLocked())
{
TRACE(_T("Thread: locked\r\n"));
g_tick++;
//char * str = (char*)malloc(101);
//memset(str, 0, 101);
//sprintf_s(str, 100, ("WorkThread: #%d"), g_tick);
CString s;
s.Format(_T("WorkThread: #%d"), g_tick);
LPCTSTR wps = (LPCTSTR)s;
WCHAR* newps = (WCHAR*)malloc(100);
wcscpy_s(newps, 40, wps);
/*
WCHAR *a = __T(str);
WCHAR *ps = (WCHAR*)malloc(101);
int size = sizeof(WCHAR)*101;
memset(ps, 0, 101);
*/
TRACE(_T("Thread: sleeping\r\n"));
Sleep(500);
TRACE(_T("Thread: un-locking\r\n"));
lock.Unlock();
TRACE(_T("Thread: un-locked\r\n"));
//PostMessage(hwnd_parent, UMSG_WORK_DATA, (WPARAM)str, 0);
SendMessage(hwnd_parent, UMSG_WORK_DATA, (WPARAM)newps, 0);
}
else
{
TRACE(_T("Thread: lock failed\r\n"));
}
Sleep(1000);
}
return 0;
}
BOOL CTestThreadDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: 在此添加专用代码和/或调用基类
if(pMsg->message == UMSG_WORK_DATA)
{
char *buf = (char*)(pMsg->wParam);
TRACE(buf);
TRACE("\r\n");
free(buf);
buf = NULL;
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
LRESULT CTestThreadDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
if(message == UMSG_WORK_DATA)
{
LPCTSTR buf = (LPCTSTR)(wParam);
m_ListBox.AddString(buf);
TRACE(buf);
TRACE("\r\n");
free((void*)buf);
buf = NULL;
}
return CDialog::WindowProc(message, wParam, lParam);
}
Test--Thread
最新推荐文章于 2022-11-04 12:19:12 发布