DLL中使用线程,自己不能销毁,请高手进来指教
在DLL中使用线程,出现了一个问题:线程不能自动销毁:
下面是两个测试线程的函数,当在CreateCommThreads和TerminateCommThreads函数执行
ShowMessageBox时,程序正常使用,而且线程能够销毁,但是当把ShowMessage屏蔽之后,
生成的生成的100个线程便不能自动销毁,请各位指教。
/****************************************************************************
测试函数组1:动态开辟数个线程
****************************************************************************/
TCommThread** TestCommThreads = NULL;
static int CreateCommThreadsNum = 100;
//---------------------------------------------------------------------------
int CreateCommThreads(int iThreads = CreateCommThreadsNum)
{
if(NULL != TestCommThreads)
{
return errThreadIsRunning;
}
TestCommThreads = new TCommThread*[iThreads];
for(int i = 0; i < iThreads; i++)
{
TestCommThreads[i] = new TCommThread(false);
TestCommThreads[i]->FreeOnTerminate = true;
//TestCommThreads[i]->Priority = tpNormal;
}
Sleep(100);
//ShowMessage("创建线程完毕");
return errOperationSuccess;
}
//---------------------------------------------------------------------------
int TerminateCommThreads(int iThreads = CreateCommThreadsNum)
{
if(NULL == TestCommThreads)
{
return errThreadIsTerminated;
}
for(int i = 0; i < iThreads; i++)
{
TestCommThreads[i]->ToBeTerminated = true;
//delete TestCommThreads[i];
TestCommThreads[i] = NULL;
}
delete[] TestCommThreads;
TestCommThreads = NULL;
//ShowMessage("over线程完毕");
return errOperationSuccess;
}
/****************************************************************************
测试函数组1:动态开辟数个线程 结束
****************************************************************************/
下面是线程的定义:
.h
//---------------------------------------------------------------------------
#ifndef FCommThreadH
#define FCommThreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//----------------------------------------------------------------------
#include "ErrorCode.h"
//---------------------------------------------------------------------------
class TCommThread : public TThread
{
private:
bool mbToBeTerminated;
bool __fastcall FToBeTerminated();
void __fastcall SetToBeTerminated(bool bValue);
protected:
void __fastcall Execute();
public:
__fastcall TCommThread(bool CreateSuspended=false);
__fastcall ~TCommThread();
__property bool ToBeTerminated = {read=FToBeTerminated, write=SetToBeTerminated, nodefault};
void __fastcall CommThreadWorks(void);
};
//---------------------------------------------------------------------------
#endif
/****************************************************************************
备注:
****************************************************************************/
下面是线程的实现:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FCommThread.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TCommThread::TCommThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
mbToBeTerminated = false;
}
//---------------------------------------------------------------------------
//设置了FreeOnTerminate属性后,会自动执行该函数
__fastcall TCommThread::~TCommThread()
{
Synchronize(CommThreadWorks);
}
//---------------------------------------------------------------------------
/****************************************************************************
__property bool ToBeTerminated = {read=FToBeTerminated, write=SetToBeTerminated, nodefault};
/****************************************************************************/
bool __fastcall TCommThread::FToBeTerminated()
{
return mbToBeTerminated;
}
//---------------------------------------------------------------------------
void __fastcall TCommThread::SetToBeTerminated(bool bValue)
{
this->mbToBeTerminated = bValue;
}
//---------------------------------------------------------------------------
//执行完该函数后,线程自动结束
void __fastcall TCommThread::Execute()
{
while(!this->mbToBeTerminated)
{
Synchronize(CommThreadWorks);
//这里还有很多工作没有作
Sleep(10);
}
return;
}
//---------------------------------------------------------------------------
void __fastcall TCommThread::CommThreadWorks(void)
{
//Application->MessageBoxA("线程执行了析构","提示",MB_OK);
}
/****************************************************************************
请各位高手指教,谢谢。
/****************************************************************************/