Delphi:在线程=“画布不允许绘制"中的ZipForge.表格的冻结 [英] Delphi: ZipForge in thread ="Canvas does not allow drawing" of Form is frozen

查看:97
本文介绍了Delphi:在线程=“画布不允许绘制"中的ZipForge.表格的冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Thread
Procedure StartUpdating.UnZip;
begin
form2.ZipForge1.FileName := ItemToExtract;
form2.ZipForge1.OpenArchive;
form2.ZipForge1.BaseDir := XXX;
form2.ZipForge1.ExtractFiles('*.*');
form2.ZipForge1.CloseArchive;
end;

PROCEDURE StartUpdating.Execute;
begin
UnZip; // on ZipForge1Password I get error: EInvalidOperation with message 'Canvas does not allow drawing'. The Form is not frozen while archive is being extracted.
Synchronize(UnZip); //  No EInvalidOperation error on ZipForge1Password, but the Form is frozen while archive is being extracted.
end;

procedure TForm2.ZipForge1Password(Sender: TObject; FileName: string;
  var NewPassword: AnsiString; var SkipFile: Boolean);
var  s:string;
begin

if PassSkip then SkipFile:=true else
    begin
    if InputQuery('Pass',FileName, s) then NewPassword:=ansistring(s) else //I suppose EInvalidOperation error is here
        begin
          PassSkip:=true;
          SkipFile:=true;
          ThreadUpdating.Terminate;
        end;
    end;
end;

如何在没有冻结格式和EInvalidOperation错误的情况下解压缩?谢谢!

How can I unzip without frozen form and without EInvalidOperation error? Thanks!

推荐答案

UnZip; // on ZipForge1Password I get error: EInvalidOperation with message 'Canvas does not allow drawing'. The Form is not frozen while archive is being extracted.

这是因为您从线程内部运行线程不安全代码(1).所有线程不安全代码(1)(像大多数VCL和WinAPI例程一样)都必须在主线程中运行. UnZip例程引用作为VCL组件的form2,因此您必须(1)使用Synchronize临时将执行转移到主线程.

This is because you run thread unsafe code (1) from within your thread. All thread unsafe code (1) (like most VCL and WinAPI routines) has to run in the main thread. The UnZip routine references form2, which is a VCL component, so you must (1) use Synchronize to temporarily transfer execution to the main thread.

Synchronize(UnZip); // No EInvalidOperation error on ZipForge1Password, but the Form is frozen while archive is being extracted.

如前所述,使用Synchronize可以在主线程中有意地执行代码,因此在提取过程中它似乎被冻结了.

As explained, with Synchronize you deliberately execute code in the main thread, hence it seems to be frozen during the extraction process.

因此,现在您遇到了一些小问题.可以通过在线程中创建ZipForge组件@runtime来消除这种情况.在这种情况下,唯一保持同步的用户交互是提供密码.缺点是Synchronize仅采用无参数方法,因此您需要做一些工作来实现OnPassword事件处理程序.它看起来可能如下所示:

So now you have a little chicken-and-egg-dilemma. One which could be eliminated by creating the ZipForge component @runtime in your thread. In that case, the only user interaction that remains being synchronized is providing a password. The downside is that Synchronize only takes a parameterless method, so you have to do a little work for implementing the OnPassword event handler. It cóuld look like the following:

type
  TUnZip = class(TThread)
  private
    FFileName: String;
    FPassword: AnsiString;
    FSkipFile: Boolean;
    procedure DoPassword;
    procedure ZipForgePassword(Sender: TObject; FileName: string;
      var NewPassword: AnsiString; var SkipFile: Boolean);
  protected
    procedure Execute; override;
  public
    property PassSkip ...
    property ItemToExtract ...
  end;

{ TUnZip }

procedure TUnZip.DoPassword;
var
  S: String;
begin
  if PassSkip then
    FSkipFile := True
  else if InputQuery('Pass', FFileName, S) then
    FPassword := AnsiString(S)
  else
  begin
    PassSkip := True;
    FSkipFile := True;
    Terminate;
  end;
end;

procedure TUnZip.Execute;
var
  ZipForge: TZipForge;
begin
  ZipForge := TZipForge.Create(...);
  try
    ZipForge.OnPassword := ZipForgePassword;
    ZipForge.FileName := ItemToExtract;
    ZipForge.OpenArchive;
    if not Terminated then  {Assuming OpenArchive triggers the OnPassword event}
    begin
      ZipForge.BaseDir := XXX;
      ZipForge.ExtractFiles('*.*');
      ZipForge.CloseArchive;
    end;
  finally
    ZipForge.Free;
  end;
end;

procedure TUnZip.ZipForgePassword(Sender: TObject; FileName: String;
  var NewPassword: AnsiString; var SkipFile: Boolean);
begin
  FFileName := FileName;
  FPassword := NewPassword;
  FSkipFile := SkipFile;
  Synchronize(DoPassword);
  FileName := FFileName;
  NewPassword := FPassword;
  SkipFile := FSkipFile;
end;

免责声明:我不熟悉ZipForge组件,因此此代码可能不完整.您应该在TUnZip.Execute中实现所有设计时生成的设置.我什至都不知道TZipForge是否有一个OnPassword事件,但是我是根据您的代码来弥补的.

Disclaimer: I am unfamiliar with the ZipForge component so this code can be incomplete. You should implement all designtime generated settings in TUnZip.Execute. I even don't know if TZipForge has an OnPassword event at all, but I made that up from your code.

(1)并非VCL的所有代码都是线程不安全的,并且并不是说从次线程中到主线程控件,组件或变量的每次调用都是危险的,但是它防止这种情况的好习惯.在我看来,线程安全一词是谨慎的警告.但是,这种 unsafety 的实际原因是,所有用户界面控件(即所有Windows GDI对象)只能对单个线程具有亲和力.主线程.

(1) Not all code of the VCL is thread unsafe, and it is not said that every call from within a secundary thread to a main thread control, component or variable is dangerous, but it is good practice to prevent it. To me, the term thread safety is comfortably cautionary. But the actual reason for this unsafety is that all user interface controls (i.e. all windows GDI objects) can only have affinity to a single thread; the main thread.

这篇关于Delphi:在线程=“画布不允许绘制"中的ZipForge.表格的冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆