tthread

Delphi TThread.CurrentThread and EAccessViolation - Is This a Bug or My Incompetence..?

耗尽温柔 提交于 2019-12-04 01:40:22
In Delphi 2009 I'm finding that any time I use TThread.CurrentThread in an application, I'll get an error message like the following when the application closes: Exception EAccessViolation in module ntdll.dll at 0003DBBA. Access violation at address 7799DBBA in module 'ntdll.dll'. Write of address 00000014. Unless it's just my machine, you can replicate this in a few seconds: create a new Delphi Forms Application, add a button to the form, and use something like the following for the button's event handler: procedure TForm1.Button1Click(Sender: TObject); begin TThread.CurrentThread; end; On

“Pausing” A Thread With A Property

淺唱寂寞╮ 提交于 2019-12-03 10:07:13
问题 I have a TThread object and want to be able to start/stop the thread via a button on the main form of the program. I've been looking into ways to do this and so far I have the following ideas: Terminate and Free the thread when the user clicks stop and create a new one when they click start. Use sleep to delay the thread (I don't want to do this) Have a property that is a boolean to determine if the thread is paused or not. The code in the Execute will only happen if this boolean is false. I

Using the Delphi XE7 Parallel Library

你说的曾经没有我的故事 提交于 2019-12-03 08:04:15
I have a time consuming routine which I'd like to process in parallel using Delphi XE7's new parallel library. Here is the single threaded version: procedure TTerritoryList.SetUpdating(const Value: boolean); var i, n: Integer; begin if (fUpdating <> Value) or not Value then begin fUpdating := Value; for i := 0 to Count - 1 do begin Territory[i].Updating := Value; // <<<<<< Time consuming routine if assigned(fOnCreateShapesProgress) then fOnCreateShapesProgress(Self, 'Reconfiguring ' + Territory[i].Name, i / (Count - 1)); end; end; end; There is really nothing complex going on. If the territory

What's wrong with using TThread.Resume? [duplicate]

被刻印的时光 ゝ 提交于 2019-12-03 06:19:56
This question already has an answer here: Resuming suspended thread in Delphi 2010? 2 answers Long ago, when I started working with threads in Delphi, I was making threads start themselves by calling TThread.Resume at the end of their constructor, and still do, like so: constructor TMyThread.Create(const ASomeParam: String); begin inherited Create(True); try FSomeParam:= ASomeParam; //Initialize some stuff here... finally Resume; end; end; Since then, Resume has been deprecated in favor to use Start instead. However, Start can only be called from outside the thread, and cannot be called from

Free a TThread either automatically or manually

时光毁灭记忆、已成空白 提交于 2019-12-03 02:59:14
I have a main thread and a separate thread in my program. If the separate thread finishes before the main thread, it should free itself automatically. If the main thread finishes first, it should free the separate thread. I know about FreeOnTerminate, and I've read that you have to be careful using it. My question is, is the following code correct? procedure TMyThread.Execute; begin ... Do some processing Synchronize(ThreadFinished); if Terminated then exit; FreeOnTerminate := true; end; procedure TMyThread.ThreadFinished; begin MainForm.MyThreadReady := true; end; procedure TMainForm.Create;

“Pausing” A Thread With A Property

耗尽温柔 提交于 2019-12-03 00:42:40
I have a TThread object and want to be able to start/stop the thread via a button on the main form of the program. I've been looking into ways to do this and so far I have the following ideas: Terminate and Free the thread when the user clicks stop and create a new one when they click start. Use sleep to delay the thread (I don't want to do this) Have a property that is a boolean to determine if the thread is paused or not. The code in the Execute will only happen if this boolean is false. I'm leaning towards #3. Would setting a boolean property on the TThread object from the main form be

Thread Error: The Handle is Invalid (6) when trying to Free a suspended thread

你离开我真会死。 提交于 2019-11-30 15:11:20
问题 In a given example I am receiving an exception when calling AThread.Free. program Project44; {$APPTYPE CONSOLE} uses SysUtils, Classes, Windows; type TMyException = class(Exception); var AThread: TThread; begin AThread := TThread.Create(True); try AThread.FreeOnTerminate := True; //I want to do some things here before starting the thread //During the setup phase some exception might occur, this exception is for simulating purpouses raise TMyException.Create('exception'); except AThread.Free;

Thread Error: The Handle is Invalid (6) when trying to Free a suspended thread

混江龙づ霸主 提交于 2019-11-30 13:42:07
In a given example I am receiving an exception when calling AThread.Free. program Project44; {$APPTYPE CONSOLE} uses SysUtils, Classes, Windows; type TMyException = class(Exception); var AThread: TThread; begin AThread := TThread.Create(True); try AThread.FreeOnTerminate := True; //I want to do some things here before starting the thread //During the setup phase some exception might occur, this exception is for simulating purpouses raise TMyException.Create('exception'); except AThread.Free; //Another exception here end; end. I have two questions: How should I free AThread instance of TThread

How to implement thread which periodically checks something using minimal resources?

南笙酒味 提交于 2019-11-30 12:07:20
I would like to have a thread running in background which will check connection to some server with given time interval. For example for every 5 seconds. I don't know if there is a good "desing pattern" for this? If I remember corretly, I've read somewehere that sleeping thread in its execute method is not good. But I might be wrong. Also, I could use normal TThread class or OTL threading library. Any ideas? Thanks. You could use an event and implement the Execute method of the TThread descendant by a loop with WaitForSingleObject waiting for the event, specifying the timeout. That way you can

delphi - terminate all the threads (TThread) on closing application

走远了吗. 提交于 2019-11-30 05:28:34
My application is a tcp/ip server, with main thread created only once & listening all the time. When new client connects, the main thread creates the new thread of TClientThread type. There is however no list of running Client threads, as that would make my app a bit complicated... is there any way to execute "terminate" method on all the threads, even if the thread is busy (in my case "busy" means it's waiting for the data, where the timeout set is about 30 sec ... so I have to kill it anyway, without waiting.)? The simple closing application seems not to run "terminate" method on the threads