暗
暗夜中独舞
Unregistered / Unconfirmed
GUEST, unregistred user!
我在多线程中使用DLL,在一个线程调用完DLL后重新回到线程循环的时候就出错了,请各位帮忙看下,主要代码如下
主线程:如果事件被触发,则将数据存入线程池中。
New(Data);
Data^.aaa:= aaa;
ThreadPool.SetData(Data);
procedure TThreadPool.SetData(var Data: pShare);
begin
EnterCriticalSection(MANAGE_TASK_CS);
try
FTaskPool.Add(Data);
Data := nil;
{唤醒}
SetEvent(FEvent);
finally
LeaveCriticalSection(MANAGE_TASK_CS);
end;
end;
procedure TThreadPool.GetData(var Data: pShare);
begin
EnterCriticalSection(MANAGE_TASK_CS);
try
if FTaskPool.Count>0 then
begin
Data := pShare(FTaskPool[0]);
FTaskPool.Delete(0);
end
else
Data := nil;
if FTaskPool.Count = 0 then
ResetEvent(FEvent);
finally
LeaveCriticalSection(MANAGE_TASK_CS);
end;
end;
//每个线程在取得数据后所做的应答,DLL可以安全退出,但是再次进入线程的时候就出错了
procedure TThreadPool.Answer(var Data: pShare);
var
do
wnInfo: TDownInfo;
AppInfoReq: TAppInfoReq;
RetInfo: array[0..100] of char;
i : integer;
begin
EnterCriticalSection(MANAGE_ANSWER_CS);
try
{设置参数}
{调用DLL}
finally
LeaveCriticalSection(MANAGE_ANSWER_CS);
end;
end;
每一个线程:
procedure TMyThread.Execute;
var
mData: pShare;
begin
while not Terminateddo
//在调用完DLL后。重新循环就出错了
begin
if FEvent>0 then
WaitForSingleObject(FEvent, INFINITE);
FGetData(mData);
if Assigned(mData) then
begin
FAnswer(mData);//线程池中的事件
end;
end;
end;
主线程:如果事件被触发,则将数据存入线程池中。
New(Data);
Data^.aaa:= aaa;
ThreadPool.SetData(Data);
procedure TThreadPool.SetData(var Data: pShare);
begin
EnterCriticalSection(MANAGE_TASK_CS);
try
FTaskPool.Add(Data);
Data := nil;
{唤醒}
SetEvent(FEvent);
finally
LeaveCriticalSection(MANAGE_TASK_CS);
end;
end;
procedure TThreadPool.GetData(var Data: pShare);
begin
EnterCriticalSection(MANAGE_TASK_CS);
try
if FTaskPool.Count>0 then
begin
Data := pShare(FTaskPool[0]);
FTaskPool.Delete(0);
end
else
Data := nil;
if FTaskPool.Count = 0 then
ResetEvent(FEvent);
finally
LeaveCriticalSection(MANAGE_TASK_CS);
end;
end;
//每个线程在取得数据后所做的应答,DLL可以安全退出,但是再次进入线程的时候就出错了
procedure TThreadPool.Answer(var Data: pShare);
var
do
wnInfo: TDownInfo;
AppInfoReq: TAppInfoReq;
RetInfo: array[0..100] of char;
i : integer;
begin
EnterCriticalSection(MANAGE_ANSWER_CS);
try
{设置参数}
{调用DLL}
finally
LeaveCriticalSection(MANAGE_ANSWER_CS);
end;
end;
每一个线程:
procedure TMyThread.Execute;
var
mData: pShare;
begin
while not Terminateddo
//在调用完DLL后。重新循环就出错了
begin
if FEvent>0 then
WaitForSingleObject(FEvent, INFINITE);
FGetData(mData);
if Assigned(mData) then
begin
FAnswer(mData);//线程池中的事件
end;
end;
end;