1.线程函数执行完return后,不需要调用ExitThread或TerminateThread退出线程.
2.多线程操作一个listctrl控件是不会卡的,很顺畅.
3.多线程如何控制线程个数呢?
DWORD dwThreadNum = 0; //总线程数DWORD WINAPI ThreadProc( LPVOID lpParameter){ CListTestThreadDlg *dlg = (CListTestThreadDlg*)lpParameter; CString strText; strText.Format(_T("%d"),dwThreadNum); int nItem = dlg->m_list.InsertItem(dlg->m_list.GetItemCount(),_T("xxx")); for (int i=0;i<=10;i++) { strText.Format(_T("%d"),i); dlg->m_list.SetItemText(0,1,strText); Sleep(1000); } dwThreadNum--; //线程要结束了,线程数量减1 //ExitThread(0); return 0;}//循环创建线程,限制线程最大数不大于10DWORD WINAPI MyCreateThreadProc( LPVOID lpParameter){ while(true) { if (dwThreadNum < 10) { DWORD dwID; HANDLE hHandle = CreateThread(0,0,ThreadProc,lpParameter,0,&dwID); CloseHandle(hHandle); dwThreadNum++; //线程数+1 } Sleep(10); } return 0;}//创建一个线程void CListTestThreadDlg::OnBnClickedOk(){ // TODO: 在此添加控件通知处理程序代码 DWORD dwID; HANDLE hHandle = CreateThread(0,0,MyCreateThreadProc,this,0,&dwID); CloseHandle(hHandle);}