<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-NZ link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:#1F497D'>Ok. I thought it might be the ProcessMessage function that was hanging, but perhaps it is simply the main thread stuck in the DLL and nothing to do with the message processing.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:#1F497D'>Thanks.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> delphi-bounces@listserver.123.net.nz [mailto:delphi-bounces@listserver.123.net.nz] <b>On Behalf Of </b>Jolyon Smith<br><b>Sent:</b> Tuesday, 29 October 2013 8:41 p.m.<br><b>To:</b> NZ Borland Developers Group - Delphi List<br><b>Subject:</b> Re: [DUG] Applicarion.ProcessMessage<o:p></o:p></span></p></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>If I have understood correctly and the DLL polling is occurring on the main thread then you don’t have the option of changing ProcessMessage once the message that triggers the DLL processing has been dispatched. As soon as that message is processed, causing the DLL to go off and do it’s thing, your VCL thread is already committed to servicing that DLL. <o:p></o:p></p></div><div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>-- <o:p></o:p></p></div><div><p class=MsoNormal>Jolyon Smith<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div></div><p><span style='color:#A0A0A8'>On Tuesday, 29 October 2013 at 19:54 , Ross Levis wrote:<o:p></o:p></span></p><blockquote style='border:none;border-left:solid windowtext 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:0cm;margin-top:5.0pt;margin-bottom:5.0pt'><div><div><div><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'>It’s actually a C++ DLL, not VCL based.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'>The DLL is polling for a window title to change in my app, and executes when it detects a change.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'>What I thought of doing was set OnMessage to nil when I update the window title, and set it back to ProcessMessage on a timer shortly afterwards. I’m not sure if that will work or not and it will be impossible to test. The DLL is in fact updating a server via the internet, and only if there is an issue updating the server does the delay occur, and I can’t emulate that situation unfortunately. Could be worth a try and send it out to users.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'>Ross.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='font-family:"Calibri","sans-serif";color:#1F497D'> </span><o:p></o:p></p><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p style='margin:0cm;margin-bottom:.0001pt'><b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> <a href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a> [<a href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>] <b>On Behalf Of </b>Jolyon Smith<br><b>Sent:</b> Tuesday, 29 October 2013 7:36 p.m.<br><b>To:</b> NZ Borland Developers Group - Delphi List<br><b>Subject:</b> Re: [DUG] Applicarion.ProcessMessage</span><o:p></o:p></p></div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p><div><div><p style='margin:0cm;margin-bottom:.0001pt'>If the DLL is also a Delphi DLL using VCL controls (as it almost certainly is) then your problem is that the VCL is simply not designed for multi-threading, in so far as all VCL control message processing code assumes that the processing is occurring in the main thread.<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>This is precisely why Synchronize() was devised, to ensure that if you had to make calls to VCL component code (adding items to list boxes, getting/setting edit control text etc) that this all occurred safely on the VCL message thread and thus would be safely serialised with any message processing that the VCL control(s) involved might be dealing with.<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Even if the VCL were thread-safe, if the DLL is creating windows in the context of the VCL thread (which it presumably is) then you also have the problem that a thread cannot process messages for windows created in <i>a different</i> thread. Each thread that creates windows is responsible for handling the message queue that the system creates <b>for that thread</b>.<o:p></o:p></p></div><div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Again, due to the nature of the VCL it is difficult - if not impossible - to create an application in Delphi that has multiple UI threads.<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>You say that things become a problem <span style='color:#1F497D'>when the DLL is doing something that takes several seconds.</span><o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>What is it that triggers the DLL to do this “something” ?<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>If it is something that occurs “internally” within the DLL, triggered by some user interaction with the UI that the DLL is providing then you are out of luck I think. But if the long running process in the DLL is triggered by a call made by your application, and if that long running process itself does not involve the DLL’s UI, then you <b>might</b> be able to make that call to the DLL in a background thread.<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>But you may have trouble even if that is possible, since it is highly unlikely that the DLL is taking care to be thread-safe itself (unless you have the source and can verify otherwise) so the results could be unpredictable.<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div></div></div><div><div><p style='margin:0cm;margin-bottom:.0001pt'>-- <o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Jolyon Smith<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div></div><blockquote style='border:none;border-left:solid windowtext 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:0cm;margin-top:5.0pt;margin-bottom:5.0pt'><div><div><p><span style='color:#A0A0A8'>On Tuesday, 29 October 2013 at 17:02 , Ross Levis wrote:</span><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><div><div><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>I’m wondering if any experts out there can help with this issue.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>I’m loading a 3<sup>rd</sup> party DLL which needs to have Windows messages sent to the DLL from my app for navigation purposes. It has been 10 years since I implemented this code which someone else gave me...</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>procedure TEngine.ProcessMessage(var Msg: tagMSG; var Handled: Boolean);</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>begin</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> if not IsChild(Engine.Handle,Msg.hwnd) then</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> try</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> Handled := IsDialogMessage(GetParent(msg.hwnd),Msg);</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> except</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> Handled := False;</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> end;</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>end;</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>Application.OnMessage := ProcessMessage;</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>Without this the DLL GUI would not show which component had the focus, which was a problem for blind users using the tab key.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>The problem: When the DLL is doing something that takes several seconds, the function above appears to hang waiting for the DLL to finish, and this causes the main thread in my app to also hang.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'>Is there any way around that, such as using a separate thread to process these messages? That would be somewhat out of my league.</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span lang=EN-US style='color:#1F497D'>Cheers,</span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span lang=EN-US style='color:#1F497D'>Ross. </span><o:p></o:p></p><p style='margin:0cm;margin-bottom:.0001pt'><span style='color:#1F497D'> </span><o:p></o:p></p></div></div><div><div><p style='margin:0cm;margin-bottom:.0001pt'>_______________________________________________<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>NZ Borland Developers Group - Delphi mailing list<o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a><o:p></o:p></p></div><div><p style='margin:0cm;margin-bottom:.0001pt'>Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></p></div></div></div></div></blockquote><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div></div></div></blockquote><div><p style='margin:0cm;margin-bottom:.0001pt'> <o:p></o:p></p></div></div></div><div><div><p class=MsoNormal>_______________________________________________<o:p></o:p></p></div><div><p class=MsoNormal>NZ Borland Developers Group - Delphi mailing list<o:p></o:p></p></div><div><p class=MsoNormal>Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><o:p></o:p></p></div><div><p class=MsoNormal>Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a><o:p></o:p></p></div><div><p class=MsoNormal>Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<o:p></o:p></p></div></div></div></blockquote><div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>