Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Delphi - Split Lines(Program not responding), not spliting file size (TTHREAD)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...