Jasson
Jasson

Reputation: 259

C# Saving/Loading richtext speed issue

I am currently trying to tackle a speed issue involving loading and saving richtext. Here are the details. In my application I have a Question class which needs to be able to store two richtexts. At the moment these two richtexts are being stored in the Question class as TextRanges. As far as saving/loading goes I am using TextRange.Load(Stream, DataFormats) and TextRange.Save(Stream, DataFormats). Everything works...

My problem is that I need to have 1000+ of these questions save/load from a file. When I do this saving/loading a file that contains these questions takes 20~ seconds. And on top of that, I would like to be able to load several of these files at once...which would leave the user waiting for possibly several minutes. I'm sure my problem is clear now.

So I started to investigate why it was taking so long to save/load a file and from a profiler I found that 80% of the time spent saving/loading a file is in the TextRange.Load and TextRange.Save methods.

So my question is...Does anyone have any suggestions or pointers to speeding up my file save/load?

I was trying to think of another way to store the richtext in both the Question class and in the file. I ran out of ideas fairly quickly but wanted to come here before I conceded. Any help is greatly appreciated. Also let me know if I need to clarify anything. Thank you!

Upvotes: 1

Views: 905

Answers (2)

MichaC
MichaC

Reputation: 2844

How much overhead is there in each call to Load and Save, is there any advantage to calling Load once for a big range versus many times for small ranges? How many times do you call Load when you load +1000 Questions? Is each act of calling Load updating a RichTextBox or a Flow Document somewhere? If not what are you doing with the TextRanges once you have them loaded?

I noticed this related thread and thought I would offer a suggestion: What if you save the byte arrays (or memory streams) in the Question class instead of the TextRange and only create the TextRange when you actually need to load your data in the RichTextBox (when the window for the question opens)?

Upvotes: 1

Kredns
Kredns

Reputation: 37231

You might look into threading. This way you could load your questions without locking up the UI. Load the first 100 and when they run out, load another 100 or so.

Upvotes: 0

Related Questions