Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i need to load large file into richtextbox,about 500mb。
The contents of the text file are as follows:
1.sldkjfwelj
2.sdgofwet
3.lisdgffhs
4.ohsdgsdlgn
.....
There are about about 10000000 lines。
How to load quickly without making the main window unresponsive?

What I have tried:

usb MemoryMappedFile to CreateViewStream.
C#
using (var stream = MemoryMap.CreateViewStream(0, viewSize, MemoryMappedFileAccess.ReadWrite))
   using (var sr = new StreamReader(stream, Encoding.Default))
   {
       this.rtbEditGcode.Text = sr.ReadToEnd();
   }

   MemoryMap.Dispose();

but it's too slow.
Posted
Updated 24-Apr-18 7:00am
Comments
BillWoodruff 24-Apr-18 0:56am    
If using a Memory Mapped File is too slow, then it is always going to be too slow.

Usually there is some criteria you can use to divide the file into chunks.
Dave Kreskowiak 24-Apr-18 13:11pm    
First, why on earth are you trying to show 10,000,000 lines of text to the user? That's completely useless. There is no reason for that.

Quote:
How to load quickly without making the main window unresponsive?

The common reason of such of behaviour is that the controls are firing several events at time. If some process takes long time, this makes window unresponsible.
You've got 2 possibilities:

1. using SuspendLayout[^] and ResumeLayout[^] methods
C#
this.rtbEditGcode.SuspendLayout();
//your code to load data to RichTextBox here
this.rtbEditGcode.ResumeLayout();


2. using Thread-Safe methods calls (recommended)
See: How to: Make Thread-Safe Calls to Windows Forms Controls | Microsoft Docs[^]
 
Share this answer
 
One thing is that you try to minimize the time that is taken to load the file and this way make the UI to feel more responsive. But another question is why you want to block access to the UI while loading the data.

The unresponsiveness is caused by the fact that you're doing something else in the thread that runs the UI and that thread cannot respond to any event that is sent to the UI. So if you want your application remain responsive while loading the data, a solution is to load the data in another thread.

So why not
- When loading starts, disable the richtextbox. This way no-one can make modifications to the control
- Do the data loading from the file in another thread, for example using BackgroundWorker Class (System.ComponentModel)[^]
- After loading is finished, update the content of the richtextbox
- Enable the richtextbox again

This way your UI could do other operations while the file is loading in the background.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900