Which is the correct way to start a suspended thread in delphi 2007?
Resume and Suspend are deprecated in Delphi 2010 and newer versions. Seems it is basically to discourage using them for thread synchronization. They are not meant for that.
Anyways, if all you want to do is resuming a thread created suspended, then calling Resume in older versions is safe.
If you need to use the same source code in both Delphi 2007 and Delphi XE, then you can use conditional compiling to avoid warning in XE.
Also, take a look at this question which is related to your question:
TThread.resume is deprecated in Delphi-2010 what should be used in place?
Yes, that is the correct way for older Delphi versions that do not have a Start
procedure.
There is nothing wrong with calling Resume on a thread that was created with the CreateSuspended parameter set to true in the constructor. (Why else would there be a CreateSuspended parameter after all?)
However, the real trouble comes in when you suspend/resume a running thread. Mainly this is due to references to open resources, such as COM objects. (For example, if you have an ADO connection object active, and a query running...it's not very ideal to suspend that thread and try to resume it later... it's obviously just not always going to work out well for you or the database connection in that scenario.)
If you are careful with your external references, then suspending/resuming a running thread becomes much safer, except for the possible race conditions that can crop up...but those are answers for many other questions...
The correct way to start a suspended thread is to never have a suspended thread in the first place.
There's a better way to create threads. If the caller must provide a value to the object in order for the class to work correctly, then don't make it optional: Require it as a parameter to the constructor. And if there's only one valid value for that parameter, then don't even make it a parameter: Just hard-code it in the constructor. (How many times have you written a thread class that only sometimes should free itself on termination? I've never seen that.)
constructor TMyThread.Create(Prop1, Prop2: Integer);
begin
inherited Create(False);
FreeOnTerminate := True;
Property1 := Prop1;
Property2 := Prop2;
end;
Then you can use the Ron Popeil method of creating threads: Just set it and forget it!
MyThread := TMyThread.Create(900, 2);
The caller doesn't have to do anything with the thread after creating it. And since it's a free-on-terminate thread, it's possible that the caller shouldn't even keep a reference to the MyThread
variable at all since the reference will become invalid as soon as the thread finishes running.
(Worried about that inherited Create(False)
line creating a thread that's going to start running before the rest of the constructor finishes running? Don't be! That was fixed in Delphi 6, over a decade ago. The thread will automatically start itself after the constructor finishes; see TThread.AfterConstruction
to see how.)