lazarus开发界面程序用线程显示进度条

lazarus开发界面程序用线程显示进度条,效果更好,以前没有另外显示线程,遇到上传文件或其他较长时间操作,界面就卡在那里,体验不好,现在另外启动线程操作,主界面就不至于卡在那里。

首先在主界面上定义线程 

type
   { TMyBackupThread }

  TMyBackupThread = class(TThread)
  private
    fStatusText: string;
    procedure ShowStatus;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: boolean);
  end;

  { TForm1 }

  TForm1 = class(TForm) 

然后在实现线程内容


{TBackupThread}

procedure TMyBackupThread.ShowStatus;
// this method is only called by Synchronize(@ShowStatus) and therefore
// executed by the main thread
// The main thread can access GUI elements, for example Form1.Caption.
begin
  writeln( fStatusText);
    form1.Memo1.Append(fStatusText);
end;

procedure TMyBackupThread.Execute;
var
  Respo: TStringStream;
  S: string;
begin
  fStatusText := 'Backup Starting ...';
  Synchronize(@Showstatus);  //If I remark this, it causes "access violation" error
    with TFPHttpClient.Create(nil) do
    try
      Respo := TStringStream.Create('');
      WriteLn(form1.Memo1.Lines.Strings[0]);
      WriteLn(Formatdatetime('yyyy-mm-dd hh:nn:ss.zzz',now));     
      FileFormPost(requrl + 'downFile', 'file', form1.Memo1.Lines.Strings[0], Respo);
      S := Respo.DataString;
      WriteLn(s);
       WriteLn(Formatdatetime('yyyy-mm-dd hh:nn:ss.zzz',now));
      form1.Memo1.Append(S);
      Respo.Destroy;     
    finally
      Free;
    end;
    fStatusText := 'Backup Completed';
    Synchronize(@Showstatus);
    MyShowThread.active:=false;
      Form3.hide;
end;

constructor TMyBackupThread.Create(CreateSuspended: boolean);
begin
  FreeOnTerminate := True;
  inherited Create(CreateSuspended);
end;                  

然后就可以在主界面里需要的地方调用了

procedure TForm1.btnUpClick(Sender: TObject);
var
  MyBackupThread : TMyBackupThread;
begin
  MyBackupThread := TMyBackupThread.Create(True); // With the True parameter it doesn't start automatically
  if Assigned(MyBackupThread.FatalException) then
    raise MyBackupThread.FatalException;
  // Here the code initialises anything required before the threads starts executing
     if Form3=nil then
     begin
  Form3:=TForm3.Create(application);
      WriteLn('TForm3.Create');
    end;
     Form3.Show;
  //Test_Dummy
  MyBackupThread.Start;
       Form3.FormProgress;
end;   

from3是做了一个动画进度条,也可不要,至少主界面不会卡在那里不动了

lazarus要展示gif动画图片,需要安装gifanim组件,只需要把文件名赋值即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于lazarus中的例子: lazarus\fpc\2.6.0\source\packages\fcl-web\examples\webdata\demo 用的extjs是3.4版,在新的extjs4.2环境下不能运行,费了好大劲,终于搞定。期间到laz英文论坛提问,可能跟我英文差有关吧,一个回答的都没有。搞定后,版主又要我进行了“ open a new issue in the bugtracker and add a diff with the changes ”。本着互助、共享的原则,整个东西放到lazarus中文社区论坛,欢迎大家不断改善源码,提高性能,并且将改善后的源码进行共享。 我的环境:winxp,lazarus1.1,fpc2.6.0,apache2.2,extjs4.2。 不要犯怵,安装配置很简单,运行这个demo一行代码都不需要编写。后面我会详细讲安装运行方法。 安装配置: 1、安装Apache。下载地址:http://www.fayea.com/apache-mirror//httpd/binaries/win32/httpd-2.2.22-win32-x86-openssl-0.9.8t.msi 其他版本我没试过,高版本的应该可以。 我安装到了D:\apache2.2 2、下载安装Extjs4.2。下载地址:http://cdn.sencha.io/ext-4.2.0-beta.zip?ref=extjs.org.cn 下载后,解压缩,文件夹复制到 D:\apache2.2\htdocs\,然后改名为Ext。 3、下载附件。 原来的lazarus demo 源码未做任何修改,直接编译即可。编译之前需要安装weblaz和lazwebextra两个包。 本文附件中已经有extgrid.exe,可以直接使用。 将extgrid.exe和users.dbf复制到 apache2.2\cgi-bin 目录下。 将附件中其余文件复制到 apache2.2\htdocs\demodbf 目录下。 好了,全部配置完成。 4、启动Apache,访问:http://localhost/demodbf/extgrid-json.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值