BOOL IsStart(CString strCurProcessName)
{
//得到本进程的ID号
DWORD dCurProcessID = GetCurrentProcessId()
HANDLE hSnapShot = NULL
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)
if(hSnapShot == NULL)
{
return FALSE
}
PROCESSENTRY32 processinfo
processinfo.dwSize = sizeof(PROCESSENTRY32)
BOOL status = Process32First(hSnapShot,&processinfo)
while(status)
{
if(strcmp(processinfo.szExeFile,strCurProcessName) == 0 /*已经启动了*/ &&\
processinfo.th32ProcessID != dCurProcessID /*但运祥不是本氏悄搭进程*/)
{
//说明已经启动了歼拿此进程
return TRUE
}
status = Process32Next(hSnapShot,&processinfo)
}
return FALSE
}
把程序名作为实参传入,然后用这个函数就可以进行判断了!
还没弄好?之前和你说过了,其实就是WaitForSingleObject和CEvent,对培槐宏于MFC,使用Afx方式创建线程有系列的方法可用,配册只要你保存了这个指针或句柄。
给你找了一段例程序,这里包含了判断和外部结束的全部方法,你不一定要创建这个类,只要保留CWinThread指针即可。
class CScrCapThread : public CWinThread{
CEvent m_eventKill
CEvent m_eventDead
static CEvent m_eventAnotherDead
BOOL IsKilling()
BOOL IsDead()
void KillThread()
virtual void Delete()
}
CScrCapThread::CScrCapThread()
:m_eventKill(FALSE,TRUE)
,m_eventDead(FALSE,TRUE)
{
m_bAutoDelete=FALSE
}
BOOL CScrCapThread::IsKilling()
{
return ::WaitForSingleObject(m_eventKill,0)==WAIT_OBJECT_0
}
BOOL CScrCapThread::IsDead()
{
return ::WaitForSingleObject(m_eventDead,0)==WAIT_OBJECT_0
}
void CScrCapThread::Delete()
{
// calling the base here won't do anything but it is a good habit
CWinThread::Delete()
// acknowledge receipt of kill notification
VERIFY(m_eventDead.SetEvent())
VERIFY(m_eventAnotherDead.SetEvent())
}
void CScrCapThread::KillThread()
{
// Note: this function is called in the context of other threads,
// not the thread itself.
// reset the m_hEventKill which signals the thread to shutdown
VERIFY(m_eventKill.SetEvent())
// allow thread to run at higher priority during kill process
SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL)
WaitForSingleObject(m_eventDead, INFINITE)
WaitForSingleObject(m_hThread, 明绝INFINITE)
// now delete CWinThread object since no longer necessary
if(m_bAutoDelete)
delete this
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)