Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Have form update while mainform process happening..



Permlink Replies: 5 - Last Post: Feb 28, 2018 2:57 PM Last Post By: Remy Lebeau (Te...
Ian Branch

Posts: 442
Registered: 9/23/99
Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 7, 2018 11:14 AM
Hi Team,
D10.2.2, 32bit App.
I have the following code..
procedure TMainForm.Button1Click(Sender: TObject);
var
  Form1:TForm1;
begin
  Form1 := TForm1.Create(Application);
  Form1.Show;
  // TLS:
  SendEmail(SMTPClient, 'myEmail@outlook.com', 'myEmail@outlook.com', 'Subject', 'Body', 'smtp-mail.outlook.com',
    'myEmail@outlook.com', 'xxx12345', 587 { SMTP TLS port, could be also 25 } , nil, nil, nil, ctTLS);
  Form1.Close;
  ....
  ....
  end;


On form1 is an Animated Gif.
My issue is that while form1 shows, nothing happens on form1 while the SendMail function is processing.
How do I get Form1 to animate while the SenMail is processing please?

Regards & TIA,
Ian
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 7, 2018 2:44 PM   in response to: Ian Branch in response to: Ian Branch
Ian Branch wrote:
On form1 is an Animated Gif.

That requires UI message processing in order to animate the GIF on the UI.

My issue is that while form1 shows, nothing happens on form1 while the SendMail function is processing.

That is because your SMTP code is blocking the main thread from processing messages.

How do I get Form1 to animate while the SenMail is processing please?

Move the SMTP code to a separate worker thread.

--
Remy Lebeau (TeamB)
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 7, 2018 4:53 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,
Thank you for your reply.
I have never worked with Threads, let alone worker threads.
Can you point me in an appropriate direction, politely :-), please.
Regards & Tks,
Ian
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 1:50 PM   in response to: Ian Branch in response to: Ian Branch
Ian Branch wrote:
Hi Remy,
Thank you for your reply.
I have never worked with Threads, let alone worker threads.
Can you point me in an appropriate direction, politely :-), please.
Regards & Tks,
Ian

I'll do, if you explain me that SendEMail call.
Is this from Indy? if yes, which unit to include?

Now for your Threading thing:
You can either do it the classical way, which would be to create a new class which
descends from TThread. You then would need to override the public method "Execute".
Put all code which should be run within that thread into that method. In your case your
SendEMail call.

The other alternative (and a bit easier and more modern) is to use the new parallel
processing libraries:

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_TTask_from_the_Parallel_Programming_Library

Greetings

Markus
Ian Branch

Posts: 442
Registered: 9/23/99
Re: Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 2:04 PM   in response to: Markus Humm in response to: Markus Humm
Hi Markus,
Let me have a look at the TTask stuff.
Regards,
Ian
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Have form update while mainform process happening..
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2018 2:57 PM   in response to: Markus Humm in response to: Markus Humm
Markus Humm wrote:
You can either do it the classical way, which would be to create a new class which
descends from TThread. You then would need to override the public method "Execute".
Put all code which should be run within that thread into that method. In your case your
SendEMail call.

The other alternative (and a bit easier and more modern) is to use the new parallel
processing libraries

TTask is not without its share of problems, though. For such a simple example, I would use a third option - TThread.CreateAnonymousThread(). Benefits of using TThread without having to manually derive from it, and less overhead than TTask. For example:

var
  Form1: TForm1 = nil;
 
procedure TMainForm.Button1Click(Sender: TObject);
begin
  if Form1 = nil then
    Form1 := TForm1.Create(Self);
  Form1.Show;
  TThread.CreateAnonymousThread(
    procedure
    begin
      // TLS:
      SendEmail(SMTPClient, 'myEmail@outlook.com', 'myEmail@outlook.com', 'Subject', 'Body', 'smtp-mail.outlook.com', 'myEmail@outlook.com', 'xxx12345', 587 { SMTP TLS port, could be also 25 } , nil, nil, nil, ctTLS);
    end
  );
  Thread.OnTerminate := ThreadTerminated;
  Thread.Start;
end;
 
procedure TMainForm.ThreadTerminated(Sender: TObject);
begin
  Form1.Close;
  // optional: FreeAndNil(Form1);
end;


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02