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





On Oct 20, 4:46 am, Land...@xxxxxxxxx wrote:
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.

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

.



Relevant Pages