请教个关于TDownLoadURL类的问题 ( 积分: 50 )

  • 主题发起人 主题发起人 colonel
  • 开始时间 开始时间
C

colonel

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么我在线程类中使用TDownLoadURL,该项线程我用了&quot;FreeOnTerminate:= True;&quot;但它怎么也不结束,有时会正常结束,还有以下代码设计合理吗?<br>TMyDownLoadThread = class(TThread)<br>private<br>FDownLoadURL: TDownLoadURL;<br>procedure DoDownloadProgress(Sender: TDownLoadURL; Progress,<br> &nbsp; &nbsp; &nbsp;ProgressMax: Cardinal; StatusCode: TURLDownloadStatus; StatusText: String;<br> &nbsp; &nbsp; &nbsp;var Cancel: Boolean);<br>public<br>property OnDownloadProgress: THeUpFileProgress read FOnDownloadProgress write FOnDownloadProgress;<br>end;<br><br><br>constructor TMyDownLoadThread .Create(const AUrl, ASaveFile: string; AutoLoad: Boolean);<br>begin<br>//....<br>FDownLoadURL.OnDownloadProgress:= Self.DoDownloadProgress;<br>//...<br>end;<br><br>procedure TMyDownLoadThread .Execute;<br>begin<br>//..<br> &nbsp;FDownLoadURL.ExecuteTarget(FDownLoadURL);//就是有时停在这里,明明文件已下载完还是不会再执行下面的代码<br> &nbsp;//..<br>end;<br><br>procedure TMyDownLoadThread .DoDownloadProgress(Sender: TDownLoadURL; Progress,<br> &nbsp;ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;<br> &nbsp;StatusText: String; var Cancel: Boolean);<br>begin<br> &nbsp;if Assigned(FOnDownloadProgress) then<br> &nbsp; &nbsp;FOnDownloadProgress(Self, StatusText, Progress, ProgressMax, FStatusCode);<br>end;
 
怎么我在线程类中使用TDownLoadURL,该项线程我用了&quot;FreeOnTerminate:= True;&quot;但它怎么也不结束,有时会正常结束,还有以下代码设计合理吗?<br>TMyDownLoadThread = class(TThread)<br>private<br>FDownLoadURL: TDownLoadURL;<br>procedure DoDownloadProgress(Sender: TDownLoadURL; Progress,<br> &nbsp; &nbsp; &nbsp;ProgressMax: Cardinal; StatusCode: TURLDownloadStatus; StatusText: String;<br> &nbsp; &nbsp; &nbsp;var Cancel: Boolean);<br>public<br>property OnDownloadProgress: THeUpFileProgress read FOnDownloadProgress write FOnDownloadProgress;<br>end;<br><br><br>constructor TMyDownLoadThread .Create(const AUrl, ASaveFile: string; AutoLoad: Boolean);<br>begin<br>//....<br>FDownLoadURL.OnDownloadProgress:= Self.DoDownloadProgress;<br>//...<br>end;<br><br>procedure TMyDownLoadThread .Execute;<br>begin<br>//..<br> &nbsp;FDownLoadURL.ExecuteTarget(FDownLoadURL);//就是有时停在这里,明明文件已下载完还是不会再执行下面的代码<br> &nbsp;//..<br>end;<br><br>procedure TMyDownLoadThread .DoDownloadProgress(Sender: TDownLoadURL; Progress,<br> &nbsp;ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;<br> &nbsp;StatusText: String; var Cancel: Boolean);<br>begin<br> &nbsp;if Assigned(FOnDownloadProgress) then<br> &nbsp; &nbsp;FOnDownloadProgress(Self, StatusText, Progress, ProgressMax, FStatusCode);<br>end;
 
看着乱, FDownLoadURL是线程的成员变量吧,是不是想搞成回调函数那样<br><br>其实<br>1、用Synchronize同步THeUpFileThread的方法就可以<br>2、让THeUpFileThread注册一个HWND,然后线程向它发送消息也可以<br><br>不知道理解对了没有
 
不在线程类里它是没事的,就用在线程类里就出这问题了
 
呵呵,不在线程和在线程对你的程序来说区别好像不小吧<br><br>这个<br>property OnDownloadProgress: THeUpFileProgress read FOnDownloadProgress write FOnDownloadProgress<br>可是线程的属性阿,这个东西这么做在线程的优势是什么呢,我的理解是把需要计算的部分放到线程里面,其他的哪些,特别是VCL的东西放代外面去,用Synchronize同步,也不影响多少效率
 
但我THeUpFileProgress 这个过程要取得线程里的对象的一些属性值,但Synchronize调用的过程只能是这样的定义TThreadMethod = procedure of object,达不到要求
 
后退
顶部