请教:线程中的代码为什么没有执行?(100分)

  • 主题发起人 主题发起人 rickyjia
  • 开始时间 开始时间
R

rickyjia

Unregistered / Unconfirmed
GUEST, unregistred user!
我定义了一个线程,在主程序中触发这个线程的时候,发现代码没有执行。代码片段如下:
定义的线程:
unit UnitUploadFileThread;
interface
uses
Classes, MsMultiPartFormData, IdHTTP, Windows, SysUtils;
type
UploadThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create(Suspended:Boolean;FileName,CurrentDirID,UserID,Url:string);
procedure postdata;
end;

var
IdHTTP1: TIdHTTP;
FFileName: string;
FCurrentDirID: string;
FUserID: string;
FUrl: string;
implementation
uses FilesUploadImpl;
procedure UploadThread.Execute;
begin
while not self.Terminateddo
begin
postdata;
end;
end;

constructor UploadThread.Create(Suspended:Boolean;FileName,CurrentDirID,UserID,Url:string);
begin
inherited Create(Suspended);
//FreeOnTerminate := true;
FFileName := FileName;
FCurrentDirID := CurrentDirID;
FUserID := UserID;
FUrl := Url;
end;
procedure UploadThread.postdata;
var
ResponseStream: TStringStream;
MultiPartFormDataStream: TMsMultiPartFormDataStream;
begin
MultiPartFormDataStream := TMsMultiPartFormDataStream.Create;
ResponseStream := TStringStream.Create('');
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;
MultiPartFormDataStream.AddFile('FieldName',FFileName,'multipart/form-data');
MultiPartFormDataStream.AddFormField('CurrentDirID', FCurrentDirID);
MultiPartFormDataStream.AddFormField('UserID', FUserID);
MultiPartFormDataStream.AddFormField('FileDisplayName', Utf8Encode(ExtractFileName(FFileName)));
MultiPartFormDataStream.PrepareStreamForDispatch;
MultiPartFormDataStream.Position := 0;
IdHTTP1.Post(FUrl, MultiPartFormDataStream, ResponseStream);
if Utf8Decode(ResponseStream.DataString) <> '' then
if FailedFileName = '' then
FailedFileName := Utf8Decode(ResponseStream.DataString)
else
FailedFileName :=FailedFileName +';'+#13+Utf8Decode(ResponseStream.DataString);
MultiPartFormDataStream.Clear;
MultiPartFormDataStream.Free;
ResponseStream.Free;
end;

end.
主程序:
procedure TFilesUpload.btnUploadClick(Sender: TObject);
begin
UploadFileThread := UploadThread.Create(false,FFileName,FParentID,FUserid,FUrl);
UploadFileThread.Resume;
end;
 
我试过了,可以运行
但是你的Execute部分不安全
加一下try..finally..end和
try..except..end
 
constructor UploadThread.Create(Suspended:Boolean;FileName,CurrentDirID,UserID,Url:string);
begin
inherited Create(Suspended);//这句改到后面去
//FreeOnTerminate := true;
FFileName := FileName;
FCurrentDirID := CurrentDirID;
FUserID := UserID;
FUrl := Url;
// inherited Create(Suspended);//挪到这里
end;
 
to muhx:
问题是,代码只运行了一部分就跳出了,没有完全执行啊,大概在
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;
这句以后就跳出了
to fansofamei:
修改哪个没有用
 
后退
顶部