我有一个函数,被我单独启动了一个线程。当用户触发某事件后,这个线程被启动。我想知道这个线程是否正在运行,如果运行则不启动线程,如果没有运行则启动线程运行。
解决方案 »
- 顶!MFC static text怎么添加下划线?
- [win32 sdk]关于sendmessage的问题
- 问一个高中数学题
- VC+ADO的错误?~~~~~~~~~!迷茫中...新手求助
- 想换工作了,两年半的COM,ATL工作经验
- 为什么类的实例不能继承?
- 求如下航路图的绘制方法?
- 怎么样在2000 Server中实现象XP那样只能并发一个连接的远程桌面连接?(解决了问题,再开贴给400分答谢)
- 请教高手!在list中设置属性LVS_EDITLABELS后可以编辑,但不能保存下来,为什么?
- mfc clistctrl控件刷新的问题
- char类型数组转换到unicode的问题。急!
- 各位老大帮帮我
下次判断这个句柄是否有效,GetExitCodeThread?BOOL GetExitCodeThread(
HANDLE hThread,
LPDWORD lpExitCode
);hThread
[in] Handle to the thread.
The handle must have the THREAD_QUERY_INFORMATION access right. For more information, see Thread Security and Access Rights.lpExitCode
[out] Pointer to a variable to receive the thread termination statusIf the specified thread has not terminated, the termination status returned is STILL_ACTIVE
因为当用户用ExitThread(STILL_ACTIVE) ;结束线程时也得到这个值,但这时线程已退出了。
WaitForSingleObject当然可以。
如果没有需求,则线程一直阻塞,直到被激活
需要使用线程的地方这样就不需要判断,每次调用SetEvent即可,但是数据还是要做好同步的
是这样吗?
LPDWORD lpExitCode;
GetExitCodeThread(m_pThread,lpExitCode);
if(lpExitCode==STILL_ACTIVE)
return;
是这样吗?
LPDWORD lpExitCode;
GetExitCodeThread(m_pThread,lpExitCode);
if(lpExitCode==STILL_ACTIVE)
return;
GetExitCodeThread(m_pThread,&dwExitCode);
if(dwExitCode==STILL_ACTIVE)
{
AfxMessageBox("return");
return;
}
而句柄是Handle ,他是对象的一个参数,是一个数
CString strExeNameToFind = strExeName;
HANDLE SnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(SnapShot==NULL)
{
//MessageBox("检测当前进程失败!");
return ;
}
SHFILEINFO shSmall;
CString str,strTemp;
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize=sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status=Process32First(SnapShot,&ProcessInfo);
int m_nProcess=0;
DWORD m_ProcessID=0;
while(Status)
{
m_nProcess++;
ZeroMemory(&shSmall,sizeof(shSmall));
//获取进程文件信息
SHGetFileInfo(ProcessInfo.szExeFile,0,&shSmall,
sizeof(shSmall),SHGFI_ICON|SHGFI_SMALLICON);
//在列表控件中添加映像名称
strTemp=ProcessInfo.szExeFile;
strTemp.MakeUpper();
strExeNameToFind.MakeUpper();
if(strTemp.Find(strExeNameToFind,0) >= 0)
{
m_ProcessID=ProcessInfo.th32ProcessID;
break;
}
//获取下一个进程的信息
Status=Process32Next(SnapShot,&ProcessInfo);
}
// TODO: Add extra validation here
if(m_ProcessID!=0)
{
;//已经运行
}
CXXThread *p = NULL;
......
//启动
if (p == NULL)
{
p = new CXXThread;
//启动
}// 停止
//
if (p != NULL)
{
// 停止工作
delete p;
p = NULL
}
如返回值为STILL_ALIVE
为存在