tag:blogger.com,1999:blog-29331675.post3206859486285077232..comments2024-03-05T17:37:00.995+01:00Comments on The Delphi Geek: OmniThreadLibrary Example #1: Beep, world!gabr42http://www.blogger.com/profile/06903558857617342477noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-29331675.post-15847264473301681982010-04-13T08:51:39.071+02:002010-04-13T08:51:39.071+02:00This is all as expected. The demo won't close ...This is all as expected. The demo won't close because the background thread is waiting in the for/sleep. Terminate will kill it but that will lead to lost memory.<br /><br />Please study other OTL examples to see how the background thread can be properly implemented.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-7560668656991729172010-04-13T08:47:55.878+02:002010-04-13T08:47:55.878+02:00Taking your suggestion, I added FThread to hold th...Taking your suggestion, I added FThread to hold the interface:<br /><br />procedure TfrmTestSimple.btnBeepClick(Sender: TObject);<br />begin<br /> FThread := CreateTask(Beep, 'Beep').Run;<br />end; <br /><br />Not it is responding with the beeps.<br /><br />But there is a problem again, it won't close until the beep finished. So I thought the thread should be terminated when the form was closing:<br /><br />procedure TfrmTestSimple.FormClose(Sender: TObject; var Action: TCloseAction);<br />begin<br /> FThread.Terminate(1000);<br />end;<br /><br />then the demo can be closed without having to wait all the beeps finished. This is all good until I added FastMM4 in the project file. It <br />reports some memory leaks. I thought it might due to that FThread is not cleared. So I managed to clear the interface reference by: <br /><br />destructor TfrmTestSimple.Destroy;<br />begin<br /> FThread := nil;<br /> inherited;<br />end;<br /><br />This reduced the memory leaks, but there are still two leaks of TOmniTask and TOmniThread.<br /><br />What I don't understand is that if I let the 30 beeps finished, then there is no memory leak.<br /><br />Why?Bohttps://www.blogger.com/profile/07358045373428558930noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-61042142449209576912010-04-08T14:30:18.228+02:002010-04-08T14:30:18.228+02:00That is because in this simple test, interface ret...That is because in this simple test, interface returned from the CreateTask is not stored anywhere.<br /><br />procedure TfrmTestSimple.btnBeepClick(Sender: TObject);<br />begin<br /> CreateTask(Beep, 'Beep').Run;<br />end;<br /><br />Therefore, this interface is destroyed when btnBeepClick exits. When the task control's destructor is called, it waits for the task to complete and that's causing the blockage.<br /><br />You should store result of the CreateTask in a form field, as the next demos do.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-80180788546434187992010-04-08T14:19:45.368+02:002010-04-08T14:19:45.368+02:00Hi,
I have tried to make it beeps more than once,...Hi,<br /><br />I have tried to make it beeps more than once, so I have modified the procedure Beep to:<br /><br />for I := 0 to 30 do<br />begin<br /> MessageBeep(MB_ICONEXCLAMATION);<br /> Sleep(1000);<br />end;<br /><br />But then I have found that the demo is not responding until the beeping finished.<br /><br />What have I done wrong?<br /><br />Bo Chen LinBohttps://www.blogger.com/profile/07358045373428558930noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-31862314765522315592008-07-10T11:10:00.000+02:002008-07-10T11:10:00.000+02:00Thanks, I'm glad you like it.Spread the good word ...Thanks, I'm glad you like it.<BR/><BR/>Spread the good word around :)gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-65671780260053507302008-07-10T10:42:00.000+02:002008-07-10T10:42:00.000+02:00Amazing threading approach.I really love it.Amazing threading approach.<BR/>I really love it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-38395028838023450622008-07-01T15:06:00.000+02:002008-07-01T15:06:00.000+02:00New version of the OtlTask unit with SetThreadName...New version of the <A HREF="http://code.google.com/p/omnithreadlibrary/source/browse/trunk/OtlTask.pas" REL="nofollow">OtlTask</A> unit with SetThreadName support was just uploaded to the Google Code.gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-72538471822112967862008-07-01T14:07:00.000+02:002008-07-01T14:07:00.000+02:00That is not a bad idea.I had problems with such co...That is not a bad idea.<BR/><BR/>I had problems with such code in older Delphis. I don't remember it well, but I think that the debugger stopped on this RaiseException when I was single-stepping through the code. I'll have to try it in D2007 ...gabr42https://www.blogger.com/profile/06903558857617342477noreply@blogger.comtag:blogger.com,1999:blog-29331675.post-52461491311050525882008-07-01T08:36:00.000+02:002008-07-01T08:36:00.000+02:00You could use this function to make the thread's n...You could use this function to make the thread's name show up in the thread list:<BR/><BR/>procedure SetThreadName(const _Name: string);<BR/>var<BR/> ThreadNameInfo: TThreadNameInfo;<BR/>begin<BR/> ThreadNameInfo.FType := $1000;<BR/> ThreadNameInfo.FName := PChar(_Name);<BR/> ThreadNameInfo.FThreadID := $FFFFFFFF;<BR/> ThreadNameInfo.FFlags := 0;<BR/> try<BR/> RaiseException($406D1388, 0, SizeOf(ThreadNameInfo) div SizeOf(LongWord), @ThreadNameInfo);<BR/> except<BR/> // ignore<BR/> end;<BR/>end;<BR/><BR/>twmAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-18693612168462302842008-07-01T08:23:00.000+02:002008-07-01T08:23:00.000+02:00True. At this moment, you can only do SVN checkout...True. At this moment, you can only do SVN checkout. Instructions are on the Source tab.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-29331675.post-46174906490559431062008-07-01T03:06:00.000+02:002008-07-01T03:06:00.000+02:00"This project currently has no downloads."----from..."This project currently has no downloads."----from google codeLeohttps://www.blogger.com/profile/17493105374708959147noreply@blogger.com