开发者

Delphi: Form becomes Frozen while assigning strings in thread

开发者 https://www.devze.com 2023-03-21 11:39 出处:网络
A code below is in a thread. Tf1 := TFileStream.Create(LogsPath,fmOpenRead or fmShareDenyNone); ... str:=TStringList.Create;

A code below is in a thread.

Tf1 := TFileStream.Create(LogsPath,fmOpenRead or fmShareDenyNone);
...
str:=TStringList.Create;
str.LoadFromStream(tf1);
...
SynEditLog.Lines.Assign(str); // I do this with Synchronize

There are 开发者_如何学JAVA30 000 strings in a text document.

A form becomes frozen while assigning those strings to SynEdit.

If to load string by string it takes me 40 sec... If to use Assign - 8 sec.

How to prevent this form's state?

Thanks!!!


I don't think Application.ProcessMessages is going to help here at all, since all the work happens in the one call to Assign.

Does SynEditLog have BeginUpdate/EndUpdate methods? I'd use them and see how you go. For instance:

SynEditLog.BeginUpdate;
try
  SynEditLog.Lines.Assign(str);
finally
  SynEditLog.EndUpdate;
end;

In response to that not working

You'll need to break down the assignment of the string list to the Lines property. Something like this:

var
  LIndex: integer; 
begin
  SynEditLog.BeginUpdate;
  try
    //added: set the capacity before adding all the strings.
    SynEditLog.Lines.Capacity := str.Capacity;
    for LIndex := 0 to str.Count - 1 do
    begin
      SynEditLog.Lines.Add(str[LIndex]);
      if LIndex mod 100 = 0 then
        Application.ProcessMessages;
    end;
  finally
    SynEditLog.EndUpdate;
  end;
end;

(note: code typed directly into browser, may not compile)

If this is too slow, try increasing the LIndex mod 100 = 0 to something larger, like 1000 or even 5000.

N@


The form is freezing because you're using the GUI thread to add 30,000 lines to your control, which naturally takes a while. During this time, the GUI can't update because you're using its thread, so it looks frozen.

One way around this would be to add a few lines (or just one) at a time, and, in between each add, update the GUI (by calling Application.ProcessMessages (thanks gordy)).

0

精彩评论

暂无评论...
验证码 换一张
取 消