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



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.

.



Relevant Pages


Quantcast