Hi!<div><br></div><div>I got the same when doing some classes for my project. My solution is add a TEvent in my thread that will be signaled in the last line of the thread loop.</div><div><br></div><div>On my application instead use TThread.WaitFor, I use TThread.MyWaitFor that internals do:</div>
<div><br></div><div>While TEvent.WaitFor(1)<>wrSignaled do</div><div> Application.ProcessMessages;<br><br></div><div>It solves my problem with threads finalization on windows and linux.</div><div><br></div><div><br>
</div><div>Fabio</div><div><br><div class="gmail_quote">2010/10/8 Graeme Geldenhuys <span dir="ltr"><<a href="mailto:graemeg.lists@gmail.com">graemeg.lists@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<br>
<br>
[Just as well the Lazarus IDE didn't get the thread update mentioned a<br>
few days ago... where a thread was doing work, and the main thread was<br>
waiting for that thread to finish... it wouldn't have worked under<br>
Linux it seems.]<br>
<br>
<br>
I just stumbled across this issue in fpGUI, and I see MSEgui and<br>
LCL-GTK2 has the<br>
exact same problem. See attached project. I haven't tried LCL-QT, but I presume<br>
that same issue will persist.<br>
<br>
A simple program (just for illustrative purposes), where a thread<br>
updates the progress bar (the thread could be doing any long running process).<br>
Click the button, it creates a thread and<br>
starts it off. If then waits for the thread to finish, then displays<br>
that it has finished in the GUI, and free's the thread manually.<br>
<br>
If you use the MyThread.WaitFor to wait for the thread to finish, then<br>
the application is instantly frozen (even the thread), nothing gets<br>
updated and you have to kill the process. For some reason<br>
WaitFor blocks the main thread's event loop. It seems this issue is<br>
only under FPC apps and under X11 (no matter the GUI toolkit),<br>
it works fine under Windows.<br>
<br>
The work around, is to use a boolean variable in the thread class, and<br>
a while loop which checks if the boolean variable is true, if not it<br>
calls Application.ProcessMessages (to keep the app alive).<br>
<br>
Like I said, this is a stupid example, but it illustrates that WaitFor<br>
cannot be used under X11 (for our GUI toolkits). Any ideas as to why<br>
WaitFor blocks the main event loop? Is it a Linux thing, X11 thing, or<br>
just that we made a mistake somewhere?<br>
<br>
So what is wrong? Is it the TThread.WaitFor implementation that is<br>
flawed? Is it X11 that is flawed, or just GUI toolkit developers that<br>
don't know how to code (thought I doubt it's the latter). :)<br>
<br>
As an experiment (I'm all out of ideas now), I dived into the<br>
TThread.WaitFor implemenation, as saw that it calls<br>
WaitForThreadTerminate() with no timeout value. I though, well, let me<br>
try calling WaitForThreadTerminate() directly with say a 2 second<br>
timeout. No luck there. The instant you call that, the application is<br>
frozen. It never times out either, so the FPC+Unix<br>
WaitForThreadTerminate() functions seems broken too. :-/<br>
<br>
I guess I need to file a FPC bug report about WaitForThreadTerminate()<br>
being broken under Linux, but I wanted others to confirm this first,<br>
before I do.<br>
<br>
Did anybody know about this? Anybody know how we can get<br>
TThread.WaitFor to actually work under Linux/Unix?<br>
<font color="#888888"><br>
<br>
--<br>
Regards,<br>
- Graeme -<br>
<br>
<br>
_______________________________________________<br>
fpGUI - a cross-platform Free Pascal GUI toolkit<br>
<a href="http://opensoft.homeip.net:8080/fpgui/" target="_blank">http://opensoft.homeip.net:8080/fpgui/</a><br>
</font><br>--<br>
_______________________________________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lists.lazarus.freepascal.org">Lazarus@lists.lazarus.freepascal.org</a><br>
<a href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus</a><br>
<br></blockquote></div><br></div>