You need to handle exceptions, although this can be done in many
different ways the following (scribbled in OE) may show one possible
way:

procedure TThreadedMail.Execute;
var 
    msg:TMsg;
    IsMessage : Boolean;
begin
   try
       mainform.SmtpCli.ThreadAttach; 
       try   
           mainform.SmtpCli.CtrlSocket.MultiThreaded := true;       
           try                  
               mainform.SmtpCli.Connect;           
               while True do
               begin
                 IsMessage := GetMessage(Msg, 0, 0, 0);
                 { Just in case someone calls WM_QUIT ?}
                 if (not IsMessage) or (msg.message = wm_mailcancel) then 
                    raise Exception.Create('Mail aborted');
                 { wm_mailCompleted - Posted from SessionClose or RequestDone }
                 if (msg.message = wm_mailCompleted) then   
                     Break;
                 TranslateMessage(Msg);
                 DispatchMessage(Msg);
               end;         
           except
               handle error;
               Better unassign event handlers;
               mainform.SmtpCli.Abort;
           end;
       finally
            mainform.SmtpCli.ThreadDetach;
       end 
   finally       
       { Has to be called in any case if you free the thread on your own }
       PostThreadMessage(mainthread, wm_mailende, 0, 0); 
   end;
end;


--
Arno Garrels [TeamICS]
http://www.overbyte.be/eng/overbyte/teamics.html


Markus Humm wrote:
> Hello,
> 
> I've tested one of my ideas for making SmtpCli working for me now.
> I've constructed a thread which will do the sending of the mail and
> thus does a thread attach/detach and has its own message loop.
> 
> It also knows the threadid of the main program so it posts a message
> to it when the thread's execute is done either by successfully
> sending the mail or because some error occured. (okay, I should add
> the errorcode to the message, just  got this idea but okay...).
> 
> The thread also has a Cancel method which sends a special message to
> the thread which also makes it leave his message loop and does a
> abort on the control socket of the smtpcli and on the smtpcli.
> 
> The one sending the e-mail fires a timer which when occurring will
> call this cancel method. If the mail sending was faster than the
> timer the timer is freed. I've tested this with a working e-mail
> server and a wrong host name. Is seemed to work, but I'd like to have
> some expert's opinion if this is good (or at least okay) practice or
> not, before I'm running into severe trouble. I'll slightly enhance it
> and test it tomorrow at work.
> 
> This is the execute (where mainform is the VCL mainform of the test
> app.): 
> 
> procedure TThreadedMail.Execute;
> var msg:TMsg;
> begin
>    mainform.SmtpCli.ThreadAttach;
>    mainform.SmtpCli.CtrlSocket.MultiThreaded:=true;
>    mainform.SmtpCli.Connect;
> 
>    msg.message:=0;
> 
>    while GetMessage(Msg, 0, 0, 0) do
>    begin
>      if (msg.message = wm_mailcancel) then break;
> 
>      TranslateMessage(Msg);
>      DispatchMessage(Msg);
>    end;
> 
>    if (Msg.message = wm_mailcancel) then
>    begin
>      try
>        mainform.SmtpCli.CtrlSocket.Abort;
>        mainform.SmtpCli.Abort;
>      except
>      end;
>    end;
> 
>    mainform.SmtpCli.ThreadDetach;
>    PostThreadMessage(mainthread, wm_mailende, 0, 0);
> end;
> 
> Greetings
> 
> Markus
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to