一个线程里每隔一段时间下载一个文件,然后在一个小窗口里显示。
因为代码不通,所以写主要部分:线程单元:
unit unMsgThread;interface
uses classes,sysutils,StdCtrls,SHDocVw,unFrmMsg,Forms,IdHTTP;type
  TnewMsgThread = class(TThread)
  private
    tmpHttp: TIdHTTP;
  protected
    procedure Execute; override;
  public
    strWebAdd:string;
  end;
implementation
uses unfrmMain;procedure TnewMsgThread.Execute;
var tmpFrm:TfrmMsg;
begin
  tmpHttp:= TIdHTTP.Create(nil);
  strWebAdd:='http://localhost/message/new.html';
  tmpFrm:=TfrmMsg.Create(nil);     //程序执行到这里就死了,为什么?
  tmpFrm.Left:=screen.Width-tmpFrm.Width;
  tmpFrm.top:=screen.WorkAreaHeight-tmpFrm.Height;
  tmpFrm.txMsg.Caption:=tmpHttp.Get(strWebAdd) ;
  tmpFrm.Show;
end;end.说明:
TfrmMsg是一个普通窗口,没任何代码,只有几个标签控件用来显示。
代码暂没加固定时间循环部分,
因为稍后要加循环,所以必须在线程里创建新窗体对象。程序运行到线程里创建窗体的时候死掉,有时候电脑都死机必须重启。
请问这个问题该如何解决?
请好心人贴上完整线程代码,及调用方法 谢谢。

解决方案 »

  1.   

    心事重重,睡不着觉,简单写一个你参考一下吧
    unit Unit2;interfaceuses
      Classes,Windows,Unit3,SysUtils,Forms;type
      TViewForm = class(TThread)
      private
        { Private declarations }
        FForm : TForm3;
        sTime : string;
      protected    procedure Execute; override;
      public
        constructor Create;overload;
        destructor Destroy; override;
      end;implementation{ Important: Methods and properties of objects in VCL or CLX can only be used
      in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure TViewForm.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TViewForm }constructor TViewForm.Create;
    begin
      inherited Create(True);
      FreeOnTerminate := True;
      FForm := TForm3.Create(nil);
      Resume;
    end;destructor TViewForm.Destroy;
    begin
      Freeandnil(FForm);
      inherited;
    end;procedure TViewForm.Execute;
    begin
      { Place thread code here }
      while not Self.Terminated do
      begin
        sTime := FormatdateTime('yyyy-dd-dd hh:mm:ss',Now);
        if not FForm.Visible then
        FForm.Show;
        FForm.Caption := sTime;
        Sleep(1000);
      end;
    end;end.
      

  2.   

    涉及到窗体的部分,使用线程同步功能!
    参考:
    procedure TMyThread.PushTheButton;begin
      Button1.Click();
    end;procedure TMyThread.Execute;
    begin
    ...
      Synchronize(PushTheButton);
      ...
    end;
      

  3.   

    Important: Methods and properties of objects in VCL or CLX can only be used 
      in a method called using Synchronize