我的程序在一个子窗口里边调用递归文件拷贝
rThread1 = CreateThread(0,0,tGetSelFileNumber,(LPVOID)this,0,&rThread);
的方法建立了一个线程线程里调用递归搜文件的函数!!!DWORD WINAPI tUpdateFile(LPVOID pPARA)
{
CDlgUpdate* p = (CDlgUpdate*)pPARA;
p->CopyServerFile(p->dir,p->num); return 1;
}当点取消键结束线程返回主窗口
ret=::TerminateThread(rThread1,1);可我发现:在我结子窗口的时候线程没了,可是会出现非法操作,好像那个函数却继续运行,请问大家有没有好的方法解决这个问题,比如说用try{} catch

解决方案 »

  1.   

    用TerminateThread结束线程的时候,该线程内的一些清理工作没法作了。
    TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating. TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems: 看看Msdn中的解释吧,建议用其它方法结束线程。
      

  2.   

    第一,你用CreatThread产生一个线程本来就不是很好,一般情况下建议用beginthreadex()
    第二,结束线程的时候用TerminateThread()线程内的一些清理工作没法作了,容易产生内存泄露和一些别的不好的影响。建议用endthreadex()
      

  3.   

    看来TerminateThread can result in the following problems。既然M$也这么说,你就改一下吧!CWinThread AFXAPI* AfxBeginThread (CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);void AFXAPI AfxEndThread (UINT nExitCode, BOOL bDelete = TRUE);