请教:如何在app中杀掉控件中的线程

qfxx 2005-03-02 08:29:11
在控件中启动了新的线程
在app中该怎么杀掉呢
请教
...全文
104 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanhill 2005-03-02
  • 打赏
  • 举报
回复
那就将控件中产生的线程的Handle传给app中,在app中terminatethread就行了
masterz 2005-03-02
  • 打赏
  • 举报
回复
bool ThreadClass::StopAll(void)
{
std::deque<DWORD> dqThreads;
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.
DWORD dwProcessID=GetCurrentProcessId();
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, dwProcessID);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return false;
// Fill in the size of the structure before using it.
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)
{
dqThreads.push_back(tie.te32.th32ThreadID);
ZeroMemory(&tie, sizeof(THREAD_INFORMATION_EX));
tie.te32.dwSize = sizeof(THREADENTRY32);
}
}
while (Thread32Next(hThreadSnap, &tie.te32));
bRet = TRUE;
}
//else
// return false; // could not walk the list of threads
CloseHandle (hThreadSnap);
//
HANDLE ThreadArray[64];
int ThreadCount = 0;
m_cs.Lock();
std::map<DWORD, ThreadClass*>::iterator it=m_mapThreads.begin();
for(;it!=m_mapThreads.end();it++)
{
if( std::find(dqThreads.begin(),dqThreads.end(),it->first)==dqThreads.end())
{
//the thread not found
m_mapThreads.erase(it,it);
}
else
{
try
{
it->second->SetStopFlag();
if(ThreadCount<64)
ThreadArray[ThreadCount++] = it->second->m_hThread;
}
catch(...)
{
}
}
}
m_cs.Unlock();
DWORD dwRet=WaitForMultipleObjects(ThreadCount,ThreadArray, TRUE, 5000);
switch(dwRet) {
case WAIT_OBJECT_0:
g_Log.Debug("%s[%d]WaitForMultipleObjects return WAIT_OBJECT_0",__FUNCTION__,__LINE__);
break;
case WAIT_ABANDONED_0:
g_Log.Debug("%s[%d]WaitForMultipleObjects return WAIT_ABANDONED_0",__FUNCTION__,__LINE__);
break;
case WAIT_TIMEOUT:
g_Log.Debug("%s[%d]WaitForMultipleObjects return WAIT_TIMEOUT",__FUNCTION__,__LINE__);
break;
default:
g_Log.Debug("%s[%d]WaitForMultipleObjects return %08x",__FUNCTION__,__LINE__,dwRet);
break;
}
return true;
}
qfxx 2005-03-02
  • 打赏
  • 举报
回复
app是使用控件的应用程序
sanhill() 你说的我明白
不过现在需要这么做罢了
sanhill 2005-03-02
  • 打赏
  • 举报
回复
app是什么东东?什么叫“在app中该怎么杀掉呢”
说明白一点。
再者,尽量不要主动杀死线程,能让它自己终止运行最好。
线程终止运行就是线程函数执行完,正常return 退出。
xuzheng318 2005-03-02
  • 打赏
  • 举报
回复
在一个进程中,你可以掌握所有的线程ID。在A线程里设置一个事件,B线程等待这个事件发生候就
关闭A线程。具体的函数terminatethread or postthreadmessage,可以查MSDN,有例子。在vckbase里也有
qfxx 2005-03-02
  • 打赏
  • 举报
回复
我在线等
问题解决了马上给分
qfxx 2005-03-02
  • 打赏
  • 举报
回复
可能说的不够详细
因为程序可能多次进入控件,
而控件中启动了新线程
我希望第二次进入控件时
将第一次启动的线程杀掉
不知道该怎么做
qfxx 2005-03-02
  • 打赏
  • 举报
回复
怎么传递阿

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧