Delphi - 拆分行(程序无响应),不分割文件大小(TTHREAD) [英] Delphi - Split Lines(Program not responding), not spliting file size (TTHREAD)

查看:639
本文介绍了Delphi - 拆分行(程序无响应),不分割文件大小(TTHREAD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望大家做得很好。
我只有一个问题,在Delphi编程...
我有一个备忘录,加载一个文件.txt,它包含大的行,如50,000Lines ....



所以我想将线分成5000线,然后将它们加载到一个新的备忘录中。例如对于第一次拆分的
,它将在新的文件文本上分割5000行,然后将其加载到新的备忘录上,加载(删除5000的文件)之后,当然这个大LINES将是45000



第二次拆分将从45,000分割5000,在大行上将为40,000,新的Lines(文件文本)为5000。



原因我为什么要在文件文本上分割行,然后加载它,因为当我将文件分解为备忘录时,程序没有回答(没有响应)。

  procedure TForm1.Button1Click(Sender:TObject); 
var count,i,X,m:integer;
begin
Memo2.Clear;
Label1.Caption:= IntToStr(Memo1.Lines.Count);
如果Memo1.Lines.Count> 5000然后
X:= 5000
其他X:= Memo1.Lines.Count;
为count:= 0 to x do
begin

Memo2.lines.add(Memo1.lines.Strings [0]);
Memo1.Lines.Delete(0);

Memo2.Text:= Trim(Memo2.text);

end;
结束

这是我用于将小线拆分成小的其他行的代码...

在备忘录中,当您有100万行时,程序将停止应答。



我添加了trim(memo2.text);删除结尾处的空行。



所以我可以如何分割线,而不是文件大小(因为它将破坏行)...如何可以做分割的大行,像我说的文件,然后加载它并删除它,然后当我重新点击一个按钮,它将执行相同的操作与其他行....



我知道我们必须使用TThread类,但是我不知道如何使用我的代码来实现...谢谢!



谢谢。

解决方案

线程不是问题的答案。您不应该为此任务需要线程。问题出在这里:

  Memo1.Lines.Delete(0); 

在循环中执行此操作对于备忘录来说非常昂贵。文本的第一部分被删除,其余部分被移动。



最好的方法是:


  1. 将整个源移动到的TStringList

  2. 将该字符串列表的前5000行处理为单个字符串。

  3. 使用 SelText:= ... 将整个字符串添加到备忘录。

完成后,用一次通话清除原始备忘录。


Hope everyone is doing great. I just have one question , in Delphi programming... I've a Memo that loads me a file .txt , and it contains big lines , like 50,000Lines....

SO I want to split thes lines to 5000line then loading them into a new Memo. for example for the first split , it will split 5000lines on a new file text then loading it on a new memo , after the load (deleting the file of 5000) , and of course it will be 45000 for the big LINES.

the second split it will split 5000 from the 45,000 , it will be 40,000 on the big lines , 5000on the new Lines (file text) .

THE REASON why i want to split the lines on file text then loading it , because the program isn't answering (not responding) when i split the files into the memo .

procedure TForm1.Button1Click(Sender: TObject);
var count,i ,X,m:integer;
begin
Memo2.Clear;
Label1.Caption:=IntToStr(Memo1.Lines.Count) ;
if Memo1.Lines.Count > 5000 then
X:=5000
else X:= Memo1.Lines.Count ;
for  count:=0 to x do
  begin

  Memo2.lines.add(Memo1.lines.Strings[0]);
  Memo1.Lines.Delete(0);

             Memo2.Text:=Trim(Memo2.text);

end;
end;

This is the code i'm using to split small Lines to small other lines ...

In a Memo , but when you have 1 million of lines , the program will stop answering.

i added the trim(memo2.text) ; To delete the blank line at the End.

So how i can do the spliting of Lines ,not of File SIZE(because it will destroy lines) ... how i can do the split of big lines like i said to file text then loading it and deleting it , then when i re-click a button it will do the same operation with the others lines ....

I know that we must using TThread class , but i don't know how to make it happen with my code... Thank's!

Thank you.

解决方案

Threading is not the answer to the problem. You should not need threads for this task. The problem is here:

Memo1.Lines.Delete(0);

Performing this in a loop is exceptionally expensive for a memo. The first part of the text is removed, and the rest moved up.

The best approach is:

  1. Move the entire source to a TStringList.
  2. Process the first 5000 lines of that string list into a single string.
  3. Add that entire string in one go to the memo using SelText := ....

When you are done, clear the original memo with one call.

这篇关于Delphi - 拆分行(程序无响应),不分割文件大小(TTHREAD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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