In fact definately,

Synchronising is a good start, however if there are chunks of memory that
you are accessing from the thread, its much tidyer to use critical sections
such as this.

(And this is from memory so excuse the syntax)

TMainform
   property XLs_ColumnSource read GetXLSColumnSource write
SetXLSColumnSource(value:twhatever);


function TMainform.GetXLSColumnSource:twhatever;
begin
    XLSCriticalSection.enter;
    result:=SMExportToXLS1.ColumnSource;
    XLSCriticalSection.leave;
end;

procedure TMainform.SetXLSColumnSource(value:twhatever);
begin
     XLSCriticalSection.enter;
     SMExportToXLS1.ColumnSource:=value;
     XLSCriticalSection.leave;
end;

and in the formcreate, you would need something like

XLSCriticalSection:=tcriticalsection.create; //where XLSCriticalSection is a
variable of the mainform.


n.b. Remembering to get and set the values using these methods instead of on
the component itself. Thus making it threadsafe.

This is a little more work and a bit more overhead, but its rock solid.

Your code would then change from.

procedure TAutosave.Execute;
..
       form2.SMExportToXLS1.ColumnSource := csDataEngine;
       form2.SMExportToXLS1.FileName := exportfilename;
..
end;

to

procedure TAutosave.Execute;
..
       form2.XLs_ColumnSource  := csDataEngine; //form2 or mainform or
wherever you keep the methods.
       form2.XLs_Filename := exportfilename;
..
end;

Again, forgive the syntax, untested, just to give you an idea of how it
works.

Matt.


----- Original Message ----- 
From: "Robert martin" <[EMAIL PROTECTED]>
To: "NZ Borland Developers Group - Delphi List" <[EMAIL PROTECTED]>
Sent: Thursday, August 12, 2004 3:57 PM
Subject: Re: [DUG]: Threads...


> Yes, most likely.  Threads cannot  / should not access the main VCL thread
> without being synchronised.  You could try passing the data from your grid
> as some sort of structure that the thread then works on.
>
>
> Rob Martin
> Software Engineer
>
> phone 03 377 0495
> fax 03 377 0496
> web www.chreos.com
> ----- Original Message ----- 
> From: "Chris Veale" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, August 12, 2004 3:43 PM
> Subject: [DUG]: Threads...
>
>
> >
> > Hi Again.
> >
> > Im trying to write an autosave function for a stringgrid ising threads
(so
> it can happen in the background)
> >
> > I have the following code to test it....
> >
> > the first TAutosave.execute procedure just hangs the application, but
the
> second one (commented out) works fine.
> >
> > Is this because Im trying to access components on the calling form?
> >
> > Any thoughts?
> >
> > Chris
> >
> >
> > *******************************
> >
> > procedure TForm2.ToolButton14Click(Sender: TObject);
> > var
> >   mythread:TAutosave;
> > begin
> >   mythread := TAutosave.Create(false);
> > end;
> >
> > ******************************
> > and in the thread itself
> >
> > procedure TAutosave.Execute;
> > begin
> >   if exportfilename <> '' then
> >   begin
> >     //just re-save to the existing filename chosen by the user
> >     try
> >       {export from string grid}
> >       form2.SMExportToXLS1.ColumnSource := csDataEngine;
> >       form2.SMExportToXLS1.FileName := exportfilename;
> >
> >       form2.SMExportToXLS1.Execute;
> >
> >       //let them know the save was successful
> >       saved := true;
> >       synchronize(showresult);
> >     except on e:exception do
> >     end;
> >   end;
> > end;
> >
> >
> > {procedure TAutosave.Execute;
> > var
> >   i:integer;
> > begin
> >   for i := 1 to 2000000 do
> >   begin
> >     inc(answer, round(abs(sin(sqrt(i)))));
> >     synchronize(showresult);
> >   end;
> > end;
> > }
> > procedure TAutosave.Showresult;
> > begin
> >   form2.RzMarqueeStatus1.Caption := 'Autosave Complete';
> > end;
> >
> > **********************************
> >
> >
> > ______________________________________________________
> >
> > The contents of this e-mail are privileged and/or confidential to the
> > named recipient and are not to be used by any other person and/or
> > organisation. If you have received this e-mail in error, please notify
> > the sender and delete all material pertaining to this e-mail.
> > ______________________________________________________
> > _______________________________________________
> > Delphi mailing list
> > [EMAIL PROTECTED]
> > http://ns3.123.co.nz/mailman/listinfo/delphi
> >
>
> _______________________________________________
> Delphi mailing list
> [EMAIL PROTECTED]
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
>
>


_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to