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


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

    首先在主界面上定义线程 

    1. type
    2.    { TMyBackupThread }
    3.   TMyBackupThread = class(TThread)
    4.   private
    5.     fStatusText: string;
    6.     procedure ShowStatus;
    7.   protected
    8.     procedure Execute; override;
    9.   public
    10.     constructor Create(CreateSuspended: boolean);
    11.   end;
    12.   { TForm1 }
    13.   TForm1 = class(TForm)

    然后在实现线程内容

    1. {TBackupThread}
    2. procedure TMyBackupThread.ShowStatus;
    3. // this method is only called by Synchronize(@ShowStatus) and therefore
    4. // executed by the main thread
    5. // The main thread can access GUI elements, for example Form1.Caption.
    6. begin
    7. writeln( fStatusText);
    8. form1.Memo1.Append(fStatusText);
    9. end;
    10. procedure TMyBackupThread.Execute;
    11. var
    12. Respo: TStringStream;
    13. S: string;
    14. begin
    15. fStatusText := 'Backup Starting ...';
    16. Synchronize(@Showstatus); //If I remark this, it causes "access violation" error
    17. with TFPHttpClient.Create(nil) do
    18. try
    19. Respo := TStringStream.Create('');
    20. WriteLn(form1.Memo1.Lines.Strings[0]);
    21. WriteLn(Formatdatetime('yyyy-mm-dd hh:nn:ss.zzz',now));
    22. FileFormPost(requrl + 'downFile', 'file', form1.Memo1.Lines.Strings[0], Respo);
    23. S := Respo.DataString;
    24. WriteLn(s);
    25. WriteLn(Formatdatetime('yyyy-mm-dd hh:nn:ss.zzz',now));
    26. form1.Memo1.Append(S);
    27. Respo.Destroy;
    28. finally
    29. Free;
    30. end;
    31. fStatusText := 'Backup Completed';
    32. Synchronize(@Showstatus);
    33. MyShowThread.active:=false;
    34. Form3.hide;
    35. end;
    36. constructor TMyBackupThread.Create(CreateSuspended: boolean);
    37. begin
    38. FreeOnTerminate := True;
    39. inherited Create(CreateSuspended);
    40. end;

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

    1. procedure TForm1.btnUpClick(Sender: TObject);
    2. var
    3. MyBackupThread : TMyBackupThread;
    4. begin
    5. MyBackupThread := TMyBackupThread.Create(True); // With the True parameter it doesn't start automatically
    6. if Assigned(MyBackupThread.FatalException) then
    7. raise MyBackupThread.FatalException;
    8. // Here the code initialises anything required before the threads starts executing
    9. if Form3=nil then
    10. begin
    11. Form3:=TForm3.Create(application);
    12. WriteLn('TForm3.Create');
    13. end;
    14. Form3.Show;
    15. //Test_Dummy
    16. MyBackupThread.Start;
    17. Form3.FormProgress;
    18. end;

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

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

  • 相关阅读:
    Java基于SSM的校园一卡通系统设计与实现
    [CSAWQual 2016]i_got_id(Perl ARGV)
    【Python基础】高级数据类型:初识 “列表” || 列表的增删改查 || del关键字 || 列表的定义
    JSR303和拦截器
    OpenCV7-copyTo截取ROI
    注解方式优雅的实现 Redisson 分布式锁
    free pascal 调用 C#程序读 Freeplane.mm文件,生成测试用例.csv文件
    HTML期末大作业(HTML+CSS+JavaScript响应式游戏资讯网站bootstrap网页)
    【PyQT5】PyQT5通用功能设置
    移动互联网的下半场,产品经理们该变了
  • 原文地址:https://blog.csdn.net/qiaozhangchi/article/details/132736715