0

I'm working on an multiThreaded server with a VCL form used for maintenance of the system. Each thread can write on a RichEdit of the MainForm to show what it does in real time (If an issue occurs, we must fix this quickly).

But, when a thread writes to the RichEdit, I from time to time get an "EOutOfResource" error: "Erreur d'insertion de ligne RichEdit" (Google translate: Error insertion line RichEdit). The RichEdit contents become a mess.

This is the main form procedure that adds add a line on the RichEdit:

procedure Main.MajRichEdit(S1,S2:string;tform:TFontStyles;i1:integer)
begin
    CriticalSection.Acquire
    if S2='C' then
    begin
        RichEditC.SelAttributes.Style:=tform;
        RichEditC.SelAttributes.Color:=i1;
        RichEditC.Lines.Add:=S1;
    end
    else if S2='CN' then
        RichEditC.Lines.Add:=S1;

    else if S2='T' then
    begin
        RichEditT.SelAttributes.Style:=tform;
        RichEditT.SelAttributes.Color:=i1;
        RichEditT.Lines.Add:=S1;
    end
    else if S2='TN' then
        RichEditT.Lines.Add:=S1;

    else if S2='S' then
    begin
        RichEditS.SelAttributes.Style:=tform;
        RichEditS.SelAttributes.Color:=i1;
        RichEditS.Lines.Add:=S1;
    end
    else if S2='SN' then
        RichEditS.Lines.Add:=S1;
    CriticalSection.Release;
end;

These are the calls from my threads:

//..Declaration Variable..//
public
    sCall1,sCall2:string;
    tFontCall1:TFontStyles;
    iNbColor1:integer;
//..Appel MajRichEdit..//
procedure Mythread.CallREMajTelforIHM;
begin
    LockIHM.BeginWrite;
    Main.MajRichEdit(sCall1,sCall2,tFontCall1,iNbColor1);
    LockIHM.EndWrite;
end;
//..Appel CallRe ..//
//My code
    sCall1:="un petit peu de français , c'est pas plus mal ;) "
    sCall2:="C";
    tFontCall1:=[fsBold]; //If i want a txt in bold ;)
    iCall1:=clRed;
    Queue(CallREMajTelforIHM);

I have similar stuff working fine (just a EAccessViolation for one little thing but i will repair that after ;) )

The Exception could be bypassed with a try/except? I do nothing in my system for change that so i don't understand why the TRichEdit policy is modified...

Jan Doggen
  • 8,154
  • 13
  • 56
  • 117
Guillaume
  • 85
  • 9
  • Not entirely sure what you're trying to accomplish here, but I would certainly use `Try..Finally` in your CriticalSection. – Andy_D Jan 28 '14 at 14:01
  • Find, thank to you , it's the try it's true but an other thing create the issue for polici and color : 'procedure Mythread.CallREMajTelforIHM; begin LockIHM.BeginWrite; Main.MajRichEdit(sCall1,sCall2,tFontCall1,iNbColor1); LockIHM.EndWrite; end;' I'must to delete LockIHm and it's ok ;) But i dont understand why Oo ? if someone know ^^ – Guillaume Jan 28 '14 at 14:48
  • Change Queue for Synchronize. You procedure get called God knows when and then it accesses those inner member variables from another thread - they can be already changed or can be in use by your worker thread- – Arioch 'The Jan 28 '14 at 15:23
  • If you can change policies I've suggest you to pack all the data into some an anonymous procedure and call it via the proper overload of main VCL thread queue or Synchronize. Or use OmniThreadLibrary and pass the data into a Mainform via the queue, and let the main form bother about bold fonts and string constants and such – Arioch 'The Jan 28 '14 at 15:26
  • I cleaned up the language, but fail to underatand what you mean with a TRichEdit 'policy'. Maybe font? – Jan Doggen Jan 28 '14 at 15:46
  • Thank you for cleanning and for tips. It's not the font , but like an other policy like Arial. I try with Try...Finally but it's doesn't good, the server have change the policy overnight. I will try your tips... And i come back for tell you if it's good ... – Guillaume Jan 29 '14 at 07:52

0 Answers0