关于richtextbox的问题 [英] A question about richtextbox

查看:96
本文介绍了关于richtextbox的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将大文件加载到richtextbox中,大约500mb。

文本文件的内容如下:

1.sldkjfwelj

2.sdgofwet

3.lisdgffhs

4.ohsdgsdlgn

.....

有大约10000000行。

如何在不使主窗口无响应的情况下快速加载?



我尝试了什么:



usb MemoryMappedFile到CreateViewStream。

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.

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.

推荐答案

引用:

如何在不使主窗口无响应的情况下快速加载?

How to load quickly without making the main window unresponsive?



这种行为的常见原因是控件触发了几个时事。如果某个过程需要很长时间,这会使窗口不负责任。

你有两种可能性:



1.使用SsspendLayout [ ^ ]和 ResumeLayout [ ^ ]方法


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

this.rtbEditGcode.SuspendLayout();
//your code to load data to RichTextBox here
this.rtbEditGcode.ResumeLayout();





2.使用线程安全方法调用( 推荐

请参阅:如何:对Windows窗体控件进行线程安全调用Microsoft Docs [ ^ ]


有一件事是你试图最小化加载文件所花费的时间这种方式使UI感觉更具响应性。但另一个问题是为什么你想在加载数据时阻止访问用户界面。



无响应的原因是你在做其他事情运行UI的线程,该线程无法响应发送到UI的任何事件。因此,如果您希望应用程序在加载数据时保持响应,则解决方案是将数据加载到另一个线程中。



为什么不呢

- 加载开始时,禁用richtextbox。这样,没有人可以对控件进行修改

- 从另一个线程中的文件加载数据,例如使用 BackgroundWorker Class(System.ComponentModel) [ ^ ]

- 加载完成后,更新richtextbox的内容

- 再次启用richtextbox



这样你的UI可以在文件加载时执行其他操作背景。
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.


这篇关于关于richtextbox的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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