NativeXml and threading

534 views Asked by At

I am tryng to get this library working with thread but I am not able to get result back to the main thread using syncronize.

No problem creating xml within excute method.

Someone have used it?


   TMyThread = class(TThread)
       private
       ADoc : TNativeXml;
        protected
         procedure Execute; override;
         procedure DoProgress;
       public
         constructor Create(CreateSuspended: Boolean);
      end;

    { TMyThread }

    constructor TMyThread.Create(CreateSuspended: Boolean);
    begin
      inherited;
    end;

procedure TMyThread.DoProgress;
begin
 formMain.meminfo.Lines.Add(ADoc.WriteToString);
end;

procedure TMyThread.Execute;
var i,j : integer;
begin
  inherited;
begin
    ADoc:= ADoc.Create(formMain);
    try
    Adoc.XmlFormat := xfReadable;
    ADoc.LoadFromFile('test.xml');
    Synchronize(DoProgress);
    finally 
       FreeAndNil(ADoc);
    end;
end;
end; 
1

There are 1 answers

3
Cosmin Prund On

Not getting the result back to the main thread or getting access violation at this line?

ADoc:= ADoc.Create(formMain);

That single line of code contains one bug and one code smell: The code smell is that you're reffering to formMain, from a thread. The bug is that you're not calling .Create() on a class name but on the variable name itself! You probably wanted:

ADoc := TNativeXml.Create(nil);