Delphi:在线程中分配字符串时,表单变为冻结 [英] Delphi: Form becomes Frozen while assigning strings in thread

查看:137
本文介绍了Delphi:在线程中分配字符串时,表单变为冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在一个线程中。

  Tf1:= TFileStream.Create(LogsPath,fmOpenRead or fmShareDenyNone); 
...
str:= TStringList.Create;
str.LoadFromStream(tf1);
...
SynEditLog.Lines.Assign(str); //我在Synchronize

的情况下执行此操作。文本文档中有30 000个字符串。



将这些字符串分配给SynEdit时,表单变为冻结状态。



如果要通过字符串加载字符串,需要40秒.. 。如果使用Assign - 8秒。



如何防止此表单的状态?

谢谢!

解决方案

我不认为 Application.ProcessMessages 会帮助因为所有的工作都发生在 Assign 的调用中。



是否 SynEditLog BeginUpdate / EndUpdate 方法?我会使用它们,看看你如何去。例如:

  SynEditLog.BeginUpdate; 
尝试
SynEditLog.Lines.Assign(str);
终于
SynEditLog.EndUpdate;
end;

对此不起作用

您需要将字符串列表的分配细分到Lines属性中。像这样:

  var 
LIndex:integer;
begin
SynEditLog.BeginUpdate;
尝试
//添加:在添加所有字符串之前设置容量。
SynEditLog.Lines.Capacity:= str.Capacity;
for LIndex:= 0 to str.Count - 1 do
begin
SynEditLog.Lines.Add(str [LIndex]);
如果LIndex mod 100 = 0,则
Application.ProcessMessages;
end;
终于
SynEditLog.EndUpdate;
end;
end;

(注意:代码直接输入浏览器,可能无法编译)

如果这太慢,请尝试将 LIndex mod 100 = 0 增加到更大的值,例如1000或甚至5000.



N @


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 30 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@

这篇关于Delphi:在线程中分配字符串时,表单变为冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆