Threading on Android Tablet causing freezes

  

I’m working on a Firemonkey application in Delphi Tokyo and decided to add a loader screen that does some animation. I have a form with a list animation that is run within one thread, and then my calls to the datasnap server runs within another thread. I’m doing it like this because I couldn’t get the animation to work if both calls wasn’t within a thread.

Now running this on the windows version works fine. Running it on both my Huawei phone and another samsung tablet works 70% of the time. The other 30% of the time it freezes and I have to kill the app. When the datasnap load is done the loader form is supposed to be freed and closed and the main panels opacity is set to 1 and I enable the panel again. I’m not sure 100% if the app freezes and if the code is not run successfully thats supposed to enable the panel again. I was able to debug it one time while not working which produced an out of memory error, but I’m unable to recreate the issue while debugging on the phone.

The idea was that when the logging button is pressed a loader screen shows some animation while the data is retrieved and then hides it again. Am I doing something wrong in the below code?

ShowLoader;

fThread := TTask.Create
(
procedure ()
begin
try
LoDataset := fmxDataModule.ServerMethods.GetLoginDetails(edtEmail.Text, edtPassword.Text);
except on E:Exception do
begin
TThread.Synchronize(TThread.CurrentThread,
procedure()
begin
ShowMessage(‘The system could not log you in. Error Details: ‘+slinebreak+slinebreak+E.Message+slinebreak+slinebreak+’Please try again.’);
HideLoader;
end
)
end;
end;

TThread.Synchronize(TThread.CurrentThread,
procedure()
begin
fmxDataModule.LoggedInUser.LoadFromDataset(LoDataset);
if fmxDataModule.LoggedInUser.CompanyID.Value > 0 then
begin
Toolbarheader.Visible := True;
lblLoginInfo.Visible := false;
lblWelcome.Text := ‘Welcome ‘ + fmxDataModule.LoggedInUser.FirstName.Value + ‘, ‘ + fmxDataModule.LoggedInUser.LastName.Value;
GoToProfilesTab.Execute;
GenerateProfiles;
pnlButtons.Visible := True;
fLoggedIn := True;
FormResize(nil);
end else
begin
lblLoginInfo.Visible := True;
lblLoginInfo.Text := ‘User does not exist, or login details invalid’;
end;
end
);

HideLoader;
end
);

fThread.Start;

Here is the code for ShowLoader:

procedure TfrmLogin.CreateLoaderForm;
begin
if Assigned(fLoader) then
FreeAndNil(fLoader);
fLoader := TfrmLoader.Create(Self);
floader.Parent := Self;
fLoader.Left := Self.Left + (Self.Width div 2) – (fLoader.Width div 2);
fLoader.Top := Self.Top + (Self.Height div 2) – (fLoader.Height div 2);
fLoader.Show;
end;

procedure TfrmLogin.ShowLoader;
begin

pnlMain.Enabled := false;
pnlMain.Opacity := 0.4;

TTask.Create (
procedure ()
begin

TThread.Queue(TThread.CurrentThread,
procedure()
begin
CreateLoaderForm
end);

end
).Start;

end;

Hiding the loader:

procedure TfrmLogin.HideLoader;
begin
pnlMain.Enabled := True;
pnlMain.Opacity := 1;
// pnlMain.Repaint;
fLoader.Visible := False;
end;

Am I missing something in the code above?

Another question is why does my form not open in the middle of the screen? I’ve tried different things, setting the position in the form properties, and manually calculating it. It always opens up top left corner on the device, but works on windows.

Comments are closed.