[DUG] Best way to make this thread safe
Alister Christie
alister at salespartner.co.nz
Thu May 17 09:31:23 NZST 2007
If you're wanting to manipulate the VCL you need to call
synchronize(aMethod), which will sync your thread to the main
application thread - the VCL is not thread-safe. In aMethod you could
do your writelog. But I'm also not much of a threading expert so
someone else may have a better method.
Alister Christie
Computers for People
Ph: 04 471 1849 Fax: 04 471 1266
http://www.salespartner.co.nz
PO Box 13085
Johnsonville
Wellington
Nick wrote:
> I got a function here I use for logging to a richedit on my main form
> (this is just in functions unit)
>
> Procedure WriteLog(sString : string; opt : LogOption);
> Begin
> With frmMain.reLog do
> begin
> If lines.Count > 200 then lines.Delete(0);
>
> lines.add(sString);
> SelStart := length(text) - (length(sString)+2);
> SelLength := length(sString);
> Case LogOption(opt) of
> loStart : begin SelAttributes.Style := [fsbold];
> SelAttributes.Color := clBlue; end;
> loNormal : begin SelAttributes.Style := [];
> SelAttributes.Color := clBlack; end;
> loError : begin SelAttributes.Style := [fsbold];
> SelAttributes.Color := clRed; end;
> loFinished: begin SelAttributes.Style := [fsbold];
> SelAttributes.Color := clBlue; lines.add(''); end;
> End;
> SelStart := Length(Text);
> Perform(EM_SCROLLCARET, 0, 0);
> end;
> End;
>
> Now I want to use that function in my threads to log things. However
> if I call it just like this
> WriteLog('Failed to connect! DB Error', loError);
> I could run into access violations
> But not quite sure the best way to do this to make it thread safe and
> use as little extra code as possible..
>
> Should I create my own function like this
> WriteLogThread('Failed to connect! DB Error', loError);
> in my TThread
> and then in that procedure just do
> criticalsection start
> WriteLog(message, st); //this calls the function in my general
> functions unit
> criticalsection end
> or could I just use critical sections in my functions unit around the
> procedure.
>
> I hope that makes sense =)
> Thanks guys.
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with
> Subject: unsubscribe
>
>
More information about the Delphi
mailing list