timer中关闭,启动线程的问题!请大家帮忙(200)

  • 主题发起人 主题发起人 pdjwind
  • 开始时间 开始时间
P

pdjwind

Unregistered / Unconfirmed
GUEST, unregistred user!
HTTPServer 接收post 消息,收到后压入全局队列TQueue。在线程中再把收到的队列中的消息 用IdHttp post出去。以上过程正常,但是碰到异常时会导致线程阻塞。在form上结束线程,在启动线程。该过程也正常。我用timer控件,判断线程时间是否超时,来决定是否重启线程。现在的问题是:当没有post消息时,重启线程正常(人为阻塞线程)但有接收post消息时,启动线程 程序会挂掉。线程代码:unit Unit1;interfaceuses Classes,SysUtils,Main,IdHTTP;type TMyPostThread = class(TThread) private { Private declarations } HTTP: TIdHTTP;
URL: string;
Response: TStringStream;
YJTThreadString : TStrings;
protected procedure Execute;
override;
public constructor Create(Suspended:Boolean;tHttp: TIdHTTP;tURL: string);
end;
implementationconstructor TMyPostThread.Create(Suspended: Boolean;tHttp: TIdHTTP;tURL: string);
begin
// CoInitialize(nil);
inherited Create(Suspended);
HTTP := tHttp;
URL :=tURL;
HTTP.Request.ContentType:='application/x-www-form-urlencoded;';
end;
procedure TMyPostThread.Execute;var i : integer;
tempItem: TPosItem;
begin
{ Place thread code here } FreeOnTerminate := true;
i:= myQueue.Count;
while true do
begin
threadTime := now;
i:= myQueue.Count;
if Terminated then
break;
if i>0 then
begin
//---begin
// HTTP := TIdHTTP.Create(nil);// HTTP.Request.ContentType:='application/x-www-form-urlencoded;';
//----end;
Response := TStringStream.Create('');
YJTThreadString:=TStringList.Create;
tempItem:= myQueue.Peek;//myQueue.popItemP;//pop YJTThreadString.Add(tempItem.ItemStr);
try try// HTTP.Post('http://221.5.151.93:8800/BossAgent/upload',YJTThreadString,Response);
HTTP.Post(URL,YJTThreadString,Response);
myQueue.popItemP;// HTTP.DisconnectSocket;
except// HTTP.DisconnectSocket;// myQueue.PushItemP(tempItem);//PushItemP(tempItem);// Push Resume;
end;
finally Response.Free;
YJTThreadString.Free;// HTTP.Free;/// end;
end;
//end if i>0 then
i:= myQueue.Count;
sleep(10);
end;
// end while true do
end;
end.
 
timer中的代码:procedure TfmHTTPServerMain.Timer1Timer(Sender: TObject);var judgeTime,temp : int64;
begin
QueueCnt.Caption:=inttostr(myQueue.Count);
judgeTime :=SecondsBetween1970(now);
temp := SecondsBetween1970(threadTime);
if (judgeTime-temp)>30 then
begin
if (cbActive.Checked) then
begin
EnterCriticalSection(Mylock);
displaymessageTimethread('线程重启');
myth.Terminate;
sleep(1000);
myth :=TMyPostThread.Create(true, Http,S_URL);
myth.Resume;
sleep(1000);
LeaveCriticalSection(Mylock);
end;
end;
end;
 
procedure TMyPostThread.Execute;var i : integer;
tempItem: TPosItem;
begin
{ Place thread code here } FreeOnTerminate := true;
i:= myQueue.Count;
while true do
这里改为 while not Terminated do
 
Timer1Timer里要先停止计时,做完再启动超时应该在线程里做吧?线程要做好异常处理啊,timer都用不到
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
732
import
I
I
回复
0
查看
589
import
I
后退
顶部