Re: [twsocket] SmtpCli and the results of some tests
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
Re: [twsocket] SmtpCli and the results of some tests
> 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" 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
Re: [twsocket] SmtpCli and the results of some tests
Hello Markus, Looks ok at first sight. But wy not create / destroy the component in the thread's Execute ? --- Rgds, Wilfried [TeamICS] http://www.overbyte.be/eng/overbyte/teamics.html http://www.mestdagh.biz Sunday, May 13, 2007, 16:25, 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
[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