Changing a variable inside of TThread from outside of the Thread.

Changing a variable inside of TThread from outside of the Thread.

Post by Landii » Sat, 21 Oct 2006 12:46:19


I'm very new to Threading and playing around with them to try and learn
them.

I've ran into an access violation when trying to change a variable in
the thread from out side the thread.

What I doing is having a thread process some large XML files. In stead
of having the main thread looked in loops I've decided to try my hand
at threading.

Here is the basic/simplified thread code

Type
MyThread = class(TThread)
private
FCancel: Boolean;
...
protected
procedure Execute; override;
public
constructor Create(APath: String; ACallBack: TSyncCallback);
procedure Cancel;
end;

implementation

constructor MyThread.Create(APath: String; ACallBack: TSyncCallback);
begin
inherited Create(True);
...
resume;
end

procedure Execute;
begin
while SomeNode <> nil do
begin
....
if FCancel then Breake;
....
end;
end

procedure Cancel;
begin
FCancel := True;
end;

When ever I do MyThread.Cancel from the forms main thread I get an
access violation when execution reaches FCancel := True;

Keep in mind I am new to Threads and mostly new to programing. But
having a blast learning.
 
 
 

Changing a variable inside of TThread from outside of the Thread.

Post by alanglloy » Sat, 21 Oct 2006 15:25:05


That's not the real code (unless that's your error). Don't type your
code in the posting, copy it.

It's not worth anyone's time being spent on a faulty bit of code
typing.

procedure Execute;

.... should at least be ...

procedure MyThread.Execute;

... what other mistake have you made ?

Note also that Delphi's (and evryone else's) convention of a class name
starting withh a "T" is a good one.

Alan Lloyd

 
 
 

Changing a variable inside of TThread from outside of the Thread.

Post by Tom de Nee » Sat, 21 Oct 2006 17:35:36

< XXXX@XXXXX.COM > schreef in bericht


Guessing that you've left out the class names in your code...
I do not see what causes the AV, but maybe I can contribute with three
remarks:
1) I would not put Resume in the creator. If you want the thread to start
upon creation, use Create(false). Check help: the parameter tells whether to
start immediately or wait until resume is called.
2) Tthread has a procedure Terminate. You call it from the main thread to
signal that it should terminate. In the execute loop then replace If Fcancel
by If Terminated. Check help.
3) there is a classic article on threads by Martin Harvey. See
http://www.yqcomputer.com/

Tom
 
 
 

Changing a variable inside of TThread from outside of the Thread.

Post by Landii » Sat, 21 Oct 2006 21:20:41

You are correct, it's not the real code, I just reduced for simplicity
(sorry Lloyd if I wasted your time, don't answer next time then) other
wise it would of been to long to post IMO.

Thanks for the tips Tom!
 
 
 

Changing a variable inside of TThread from outside of the Thread.

Post by Rob Kenned » Sun, 22 Oct 2006 00:28:58


In certain versions of Delphi, that won't work. In those versions, when
you use CreateSuspended = False, the thread begins running as soon as
you call the inherited constructor. Thus you need to be careful about
where you call the inherited constructor. If you call it before all the
fields are set, then you have a race condition.

In later versions of Delphi, TThread will delay starting the thread
until the AfterConstruction call, even when CreateSuspended is False, so
it no longer matters when the inherited constructor gets called.

--
Rob