我有一个函数,被我单独启动了一个线程。当用户触发某事件后,这个线程被启动。我想知道这个线程是否正在运行,如果运行则不启动线程,如果没有运行则启动线程运行。

解决方案 »

  1.   

    你启动线程时应该保存一个句柄。
    下次判断这个句柄是否有效,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
      

  2.   

    STILL_ACTIVE表示线程还在运行,否则线程已经退出。
      

  3.   

    用WaitForSingleObject()也可以,本人觉得比GetExitCodeThread()要好,
    因为当用户用ExitThread(STILL_ACTIVE) ;结束线程时也得到这个值,但这时线程已退出了。
      

  4.   

    应该避免ExitThread(STILL_ACTIVE).
    WaitForSingleObject当然可以。
      

  5.   

    建议在线程的开始地方使用WaitForSingleOjbect
    如果没有需求,则线程一直阻塞,直到被激活
    需要使用线程的地方这样就不需要判断,每次调用SetEvent即可,但是数据还是要做好同步的
      

  6.   

    真是不好意思,但是具体怎么用?
    是这样吗?
    LPDWORD lpExitCode;
    GetExitCodeThread(m_pThread,lpExitCode);
    if(lpExitCode==STILL_ACTIVE)
       return;
      

  7.   

    真是不好意思,但是具体怎么用?
    是这样吗?
    LPDWORD lpExitCode;
    GetExitCodeThread(m_pThread,lpExitCode);
    if(lpExitCode==STILL_ACTIVE)
       return;
      

  8.   

    DWORD dwExitCode;
    GetExitCodeThread(m_pThread,&dwExitCode);
    if(dwExitCode==STILL_ACTIVE)
    {
    AfxMessageBox("return");
    return;
    }
      

  9.   

    那个线程的handle我可以这样定义吗?CWinThread * m_pThread;
      

  10.   

    CWinThread 是一个类,所以你定义的 *m_pThread是一个对象指针,不是句柄,
    而句柄是Handle ,他是对象的一个参数,是一个数
      

  11.   

    m_pThread->m_hThread is the handle you want.
      

  12.   

    检测是否运行:
    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)
    {
    ;//已经运行
    }
      

  13.   

    sxy说的是进程挖,阿楼主谈论线程哟
      

  14.   

    太复杂了,
    CXXThread *p = NULL;
    ......
    //启动
    if (p == NULL)
    {
       p = new CXXThread;
       //启动
    }// 停止
    // 
    if (p != NULL)
    {
    // 停止工作 
    delete p;
    p = NULL
    }
      

  15.   

    GetExitCodeThread
    如返回值为STILL_ALIVE
    为存在