简体   繁体   English

Delphi XE5 Android Thread使应用程序崩溃

[英]Delphi XE5 Android Thread makes app crash

I have a thread downloading and saving a clientDataset file cds file from a remote Datasnap Server. 我有一个线程从远程Datasnap Server下载并保存clientDataset文件cds文件。

This thread works in Windows, IOS (Simulator, Iphone, Ipad) but not on Android. 此线程适用于Windows,IOS(模拟器,Iphone,Ipad),但不适用于Android。

The app crashes after completed the file download and save it on Android. 应用程序在完成文件下载后崩溃并将其保存在Android上。

The code is simple. 代码很简单。

Thread: 线:

TDownloadSaveRemoteDBThread = class (TThread)
public
  Username,Password,host,port : String;
  error                      : String;
  Folder                     : String;
  ServerMethod               : String;
  filename                   : String;
protected
  procedure Execute; override;
end;

procedure TDownloadSaveRemoteDBThread.Execute;
var
  sqlCon : TSqlConnection;
  cds    : TClientDataset;
  ssm    : TSqlServerMethod;
  dpr    : TDataSetProvider;
begin
  inherited;

  error :='';
  SqlCon:=TSQLConnection.Create(nil);
  try
    sqlCon.DriverName:='Datasnap';
    sqlCon.LoginPrompt:=false;
    sqlCon.Params.Values['hostname']:=host;
    sqlCon.Params.Values['port']:=port;
    sqlCon.Params.Values['UserName']:=Username;
    sqlCon.Params.Values['Password']:=Password;
    try
      sqlCon.Open;
      cds:=TClientDataset.Create(nil);
      ssm:=TSqlServerMethod.Create(nil);
      dpr:=TDataSetProvider.Create(nil);
      try
        ssm.SQLConnection:=SqlCon;
        ssm.ServerMethodName:=ServerMethod;
        dpr.DataSet:=ssm;
        cds.SetProvider(dpr);
        cds.Open;
        cds.SaveToFile(folder+filename);
        cds.Close;
      finally
        cds.free;
        ssm.free;
        dpr.free;
      end;
    Except
      on E : Exception do
      Begin
        error:=E.Message;
        SQLCon.Close;
      End;
    end;
  finally
    SQLCon.Close;
    SQLCon.free;
  end;
end;

I have a button to start the thread in my mainform “Start Form” 我有一个按钮来启动我的主窗体“开始窗体”中的线程

procedure TStartForm.Button1Click(Sender: TObject);
var
  DownloadSaveRemoteDBThread : TDownloadSaveRemoteDBThread;
begin
  DownloadSaveRemoteDBThread:= TDownloadSaveRemoteDBThread.Create(true);
  DownloadSaveRemoteDBThread.OnTerminate:= StartForm.ferdigHentDB;
  DownloadSaveRemoteDBThread.Password:='test';
  DownloadSaveRemoteDBThread.Username:='test';
  DownloadSaveRemoteDBThread.host:=edit1.text; //127.0.0.1
  DownloadSaveRemoteDBThread.port:='211';
  DownloadSaveRemoteDBThread.Folder:=System.IOUtils.TPath.GetDocumentsPath +  PathDelim +'db'+  PathDelim;
  DownloadSaveRemoteDBThread.ServerMethod:= 'TServerMethods2.hentselect3';
  DownloadSaveRemoteDBThread.filename:='select3.cds';
  DownloadSaveRemoteDBThread.FreeOnTerminate:=true;
  DownloadSaveRemoteDBThread.Start;
end;

Then after thread is finished I have this simple procedure 然后在线程完成后我有这个简单的过程

procedure TStartForm.ferdigHentDB(sender: Tobject);
begin

  with sender as TDownloadSaveRemoteDBThread do
  Begin
    if error > '' then
    Begin
      showmessage(error);
    End;
  End;
end;

Any suggestions why Android crashes, is there an easy fix for this issue? 有关安卓崩溃的任何建议,是否可以轻松解决此问题?

TThread in XE5 has two major bugs on Android. XE5中的TThread在Android上有两个主要错误。 Sychronize() and Queue() are broken ( Synchronize() is used to trigger the OnTerminate event), and TThread does not detach itself from the Android JVM before termination if any JNI objects are used by the thread. Sychronize()Queue()被破坏( Synchronize()用于触发OnTerminate事件),如果线程使用了任何JNI对象,则TThread在终止之前不会从Android JVM分离。 The first bug has not been fixed yet. 第一个bug尚未修复。 The second bug is fixed in XE6, but there is a workaround you can use in XE5. 第二个错误已在XE6中修复,但您可以在XE5中使用解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM