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.
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.