> 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.

Instead of wm_mailcancel, you can use standard WM_QUIT which makes 
GetMessage return false so it breaks the message loop.

Maybe you could create the SMTP component in the execute method and destroy 
it before exiting the Execute method. You'll get a fresh new copy each time 
your thread run. A little bit slower but probably safer about the state of 
the component.

You Execute method should use a try/except englobing all the method to avoid 
having exception going out of the procedure which give all kind of problems. 
Should an exception occur, you can post a message to the main thread and let 
the worker thread gracefuly shutdown, and of course close all opened 
resources such as sockets or you'll leak resources and memory.

--
Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html
--
[EMAIL PROTECTED]
http://www.overbyte.be



----- Original Message ----- 
From: "Markus Humm" <[EMAIL PROTECTED]>
To: "ICS support mailing" <twsocket@elists.org>
Sent: Sunday, May 13, 2007 4:25 PM
Subject: [twsocket] SmtpCli and the results of some tests


> 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 

-- 
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