我的程序用的是socket其中有对数据库操作用的是ado程序在刚进行没有开始网络或其它服务功能时.线程数为1.也就是只有自身一个进程.在程序启运并监听开始后.有用户连入,这时我再停止服务.关闭了我所开的所有线程.并关闭了所有的socket,关闭了数据库.并将这些资源释放.这时再看线程.不但有自身.还多了两个.请教各位.
是我程序没写好的源因吗?
还是这两个是否我无法关闭?
还是这两个没必要关闭?最主要能否告诉我这两个线程是如何产生的吗?谢谢.......
是我程序没写好的源因吗?
还是这两个是否我无法关闭?
还是这两个没必要关闭?最主要能否告诉我这两个线程是如何产生的吗?谢谢.......
他们可能是socket或COM建立的辅助线程。
你可以嵌入以下代码随时察看自己程序的thread number
typedef struct _THREAD_INFORMATION_EX
{
_THREAD_INFORMATION_EX()
{
ZeroMemory((LPVOID)&te32, sizeof(THREADENTRY32));
ZeroMemory((LPVOID)&ctx, sizeof(CONTEXT));
} THREADENTRY32 te32;
CONTEXT ctx;
} THREAD_INFORMATION_EX;void CSaveIconDlg::OnBnClickedReportthreads()
{
DWORD dwProcessID=GetCurrentProcessId();
BOOL bGetContext = false;
HANDLE hThreadSnap = NULL;
BOOL bRet = FALSE;
THREAD_INFORMATION_EX tie;
DWORD dwThisThread = ::GetCurrentThreadId(); // used for not killing ourself // Take a snapshot of all threads currently in the system.
std::ostringstream oss;
int threadcount = 0;
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwProcessID);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return ;
tie.te32.dwSize = sizeof(THREADENTRY32);
// Walk the thread snapshot to find all threads of the process.
// If the thread belongs to the process, add its information
// to the display list.
if (Thread32First(hThreadSnap, &tie.te32))
{
do
{
if (tie.te32.th32OwnerProcessID == dwProcessID)
{
oss<<"ThreadID: "<< tie.te32.th32ThreadID<<std::endl;
threadcount++;
HANDLE hthread=OpenThread(THREAD_QUERY_INFORMATION,FALSE, tie.te32.th32ThreadID);
if(hthread)
{
CloseHandle(hthread);
}
ZeroMemory(&tie, sizeof(THREAD_INFORMATION_EX));
tie.te32.dwSize = sizeof(THREADENTRY32);
}
}
while (Thread32Next(hThreadSnap, &tie.te32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of threads
CloseHandle (hThreadSnap);
oss<<"total thread count of process "<<dwProcessID<< " is "<<threadcount<<std::endl;
OutputDebugString(oss.str().c_str());
}