L
luckystart
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我写的一段源码。大家一起看看,有没有不对的地方?
运行的时候好像不太稳定。线程创建/删除次数少的时候挺管用的。
次数多了,好像就会出问题了。
procedure TMyForm.createNewThread;
var MyThreadList:TList;
begin
MyThreadList:=ThreadList.lockList;
MyThreadList.Add(MyThread.Create);
MyThread(MyThreadList[MyThreadList.Count-1]).Priority:=tpIdle;
MyThread(MyThreadList[MyThreadList.Count-1]).FreeOnTerminate:=True;
MyThread(MyThreadList[MyThreadList.Count-1]).onTerminate:=onThTerminate;
MyThread(MyThreadList[MyThreadList.Count-1]).Resume;
ThreadList.UnlockList;
end;
procedure MyForm.OnThreadTerminate(Sender:Tobject);
var MyThreadList:TList;
begin
MyThreadList:=ThreadList.Locklist;
MyThreadList.remove(sender);
ThreadList.unLockList;
end;
运行的时候好像不太稳定。线程创建/删除次数少的时候挺管用的。
次数多了,好像就会出问题了。
procedure TMyForm.createNewThread;
var MyThreadList:TList;
begin
MyThreadList:=ThreadList.lockList;
MyThreadList.Add(MyThread.Create);
MyThread(MyThreadList[MyThreadList.Count-1]).Priority:=tpIdle;
MyThread(MyThreadList[MyThreadList.Count-1]).FreeOnTerminate:=True;
MyThread(MyThreadList[MyThreadList.Count-1]).onTerminate:=onThTerminate;
MyThread(MyThreadList[MyThreadList.Count-1]).Resume;
ThreadList.UnlockList;
end;
procedure MyForm.OnThreadTerminate(Sender:Tobject);
var MyThreadList:TList;
begin
MyThreadList:=ThreadList.Locklist;
MyThreadList.remove(sender);
ThreadList.unLockList;
end;