C# 保存/加载富文本速度问题

发布于 2024-07-17 22:28:12 字数 624 浏览 4 评论 0原文

我目前正在尝试解决涉及加载和保存富文本的速度问题。 以下是详细信息。 在我的应用程序中,我有一个 Question 类,它需要能够存储两个富文本。 目前,这两个富文本作为 TextRanges 存储在 Question 类中。 就保存/加载而言,我正在使用 TextRange.Load(Stream, DataFormats) 和 TextRange.Save(Stream, DataFormats)。 一切正常...

我的问题是我需要从文件中保存/加载 1000 多个这样的问题。 当我执行此操作时,保存/加载包含这些问题的文件需要 20~ 秒。 最重要的是,我希望能够一次加载其中几个文件......这将使用户可能等待几分钟。 我确信我的问题现在已经清楚了。

因此,我开始调查为什么保存/加载文件需要这么长时间,并且从探查器中我发现 80% 的保存/加载文件时间花在 TextRange.Load 和 TextRange.Save 方法上。

所以我的问题是...有人对加快我的文件保存/加载速度有任何建议或指示吗?

我试图想出另一种方法来将富文本存储在 Question 类和文件中。 我很快就没有了主意,但想在承认之前来到这里。 任何帮助是极大的赞赏。 如果我需要澄清任何事情,也请告诉我。 谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-07-24 22:28:12

每次调用 Load 和 Save 会产生多少开销?对于大范围调用一次 Load 与小范围调用多次相比是否有优势? 当你加载+1000个问题时,你调用了多少次Load? 调用 Load 的每个行为是否都会在某处更新 RichTextBox 或 Flow Document? 如果不是,加载 TextRanges 后您将如何处理它们?

我注意到这个相关的 线程 并认为我会提供建议:如果您将字节数组(或内存流)保存在 Question 类中而不是 TextRange 中,并且仅在实际需要在 RichTextBox 中加载数据时(当问题的窗口打开时)才创建 TextRange,会怎么样?

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)?

_畞蕅 2024-07-24 22:28:12

您可以查看线程。 这样您就可以加载问题而无需锁定用户界面。 加载前 100 个,当它们用完时,加载另外 100 个左右。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文