第一次采用多线程编程,碰到问题,请教下大家(50)

M

messah

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit2;interfaceuses Classes,Registry;type do
wnload = class(TThread) private { Private declarations } public constructor create;
protected procedure Execute;
override;
end;
implementationuses Unit1;{ Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption);
and UpdateCaption could look like, proceduredo
wnload.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
or Synchronize( procedure begin
Form1.Caption := 'Updated in thread via an anonymous method' end ) );
where an anonymous method is passed. Similarly, the developer can call the Queue method with similar parameters as above, instead passing another TThread class as the first parameter, putting the calling thread in a queue with the other thread. }{do
wnload }constructordo
wnload.create;
begin
inherited Create(True);
end;
proceduredo
wnload.Execute;var savefilestream:TFilestream;
Reg:TRegistry;
begin
tryForm1.Timer1.Enabled:=False;SaveFileStream := TFileStream.Create('tmp.exe',fmCreate);Form1.idhttp1.Create(nil);Form1.idhttp1.Request.URL:=Form1.url;Form1.idhttp1.Connected;Form1.idhttp1.Get(Form1.url,SaveFileStream);SaveFileStream.Free;exceptForm1.Close;
end;
{ Place thread code here }end;
end.
线程代码如上,在主程序中用download.create调用。可是出错。不下载文件,请教大家我哪里错了。谢谢
 
你可以先参考下例程,你这样写的肯定不行,就算行也效率太低了.
 
我就是参考例程写的啊。哪里不行呢。先解决行不行的问题再来解决效率的问题,不要一上来就说不行。我也知道不行啊。行的话我都不会发上来请教大家了。
 
proceduredo
wnload.Execute;var savefilestream:TFilestream;
Reg:TRegistry;
begin
tryForm1.Timer1.Enabled:=False;SaveFileStream := TFileStream.Create('tmp.exe',fmCreate);Form1.idhttp1.Create(nil);<----------你这句是什么意思?意图是什么?Form1.idhttp1.Request.URL:=Form1.url;Form1.idhttp1.Connected;Form1.idhttp1.Get(Form1.url,SaveFileStream);SaveFileStream.Free;exceptForm1.Close;
end;
{ Place thread code here }end;
 
应该使用同步Synchronize把你这些移到一个方法里面,就不报错了,另外最好是create(false)立即执行,而且要线程用后就自己释放才比较妥善
 
顶部