积极答复者
richTextBox异步加载txt文档错误

问题
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using System.Threading; namespace 多线程异步加载 { public partial class 异步 : Form { public 异步() { InitializeComponent(); } public delegate void UpdateTxt(string msg); public void UpdateTxtMethod(string msg) { richTextBox1.AppendText(msg+"\n\r"); richTextBox1.ScrollToCaret(); } public UpdateTxt upd; public void ThreadMethodTxt() { string[] lines = System.IO.File.ReadAllLines(@"D:\TestTxtFileLoad.txt", Encoding.Default); for (int i = 0; i < lines.Length; i++) { this.BeginInvoke(upd, lines[i]); } } private void 异步_Load(object sender, EventArgs e) { upd = new UpdateTxt(UpdateTxtMethod); Thread th = new Thread(new ThreadStart(ThreadMethodTxt)); th.Start(); } } }
this.BeginInvoke(upd, lines[i]);//报错
上下文“0x1a2c28”已断开连接。正在从当前上下文(上下文 0x1a2ab8)释放接口。这可能会导致损坏或数据丢失。要避免此问题,请确保在应用程序全部完成 RuntimeCallableWrapper (表示其内部的 COM 组件)之前,所有上下文/单元都保持活动状态。
答案
-
您用的读文件的方法 System.IO.File.ReadAllLines 不合适,要用Stream,可以有以下二个参考
1 MSDN https://msdn.microsoft.com/en-us/library/mt674879.aspx
private async Task<string> ReadTextAsync(string filePath) { using (FileStream sourceStream = new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read,
bufferSize: 4096, useAsync: true))
{
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[0x1000];
int numRead;
while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
string text = Encoding.Unicode.GetString(buffer, 0, numRead);
sb.Append(text);
}
return sb.ToString();
}
}或是 https://blogs.msdn.microsoft.com/csharpfaq/2012/01/23/using-async-for-file-access/
2 CodeProject
http://www.codeproject.com/Articles/15181/Asynchronous-file-IO-using-anonymous-methods
FileStream fs = new FileStream("C:\\sample.txt",FileMode.Open);
fs.BeginRead(data,0,data.Length,delegate(IAsyncResult ar) { /* Section 1...some code*/ },null);
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 倭黑猩猩 2016年9月21日 1:22
全部回复
-
您用的读文件的方法 System.IO.File.ReadAllLines 不合适,要用Stream,可以有以下二个参考
1 MSDN https://msdn.microsoft.com/en-us/library/mt674879.aspx
private async Task<string> ReadTextAsync(string filePath) { using (FileStream sourceStream = new FileStream(filePath,
FileMode.Open, FileAccess.Read, FileShare.Read,
bufferSize: 4096, useAsync: true))
{
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[0x1000];
int numRead;
while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
string text = Encoding.Unicode.GetString(buffer, 0, numRead);
sb.Append(text);
}
return sb.ToString();
}
}或是 https://blogs.msdn.microsoft.com/csharpfaq/2012/01/23/using-async-for-file-access/
2 CodeProject
http://www.codeproject.com/Articles/15181/Asynchronous-file-IO-using-anonymous-methods
FileStream fs = new FileStream("C:\\sample.txt",FileMode.Open);
fs.BeginRead(data,0,data.Length,delegate(IAsyncResult ar) { /* Section 1...some code*/ },null);
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 倭黑猩猩 2016年9月21日 1:22
-
同步和异常的比较可以看这里
http://stackoverflow.com/questions/18331349/c-sharp-4-5-file-read-performance-sync-vs-async
结论是异步读取文件,在执行速度上并没有多少改善
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
-
把richTextBox1.ScrollToCaret();注销掉就行了,这句没用。另外最好直接打开流读取。
using System; using System.IO; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void UpdateTxt(string msg); public void UpdateTxtMethod(string msg) { richTextBox1.AppendText(msg + "\n\r"); //richTextBox1.ScrollToCaret(); } public UpdateTxt upd; public void ThreadMethodTxt() { using (StreamReader sr = File.OpenText(@"D:\TestTxtFileLoad.txt")) { string s = ""; while ((s = sr.ReadLine()) != null) { this.BeginInvoke(upd, s); } } } private void OnLoaded(object sender, EventArgs e) { upd = new UpdateTxt(UpdateTxtMethod); Thread th = new Thread(new ThreadStart(ThreadMethodTxt)); th.Start(); } } }
没有报错了,richTextBox1.ScrollToCaret();//将控件内容滚动至插入符位置,不清楚什么原因报错?能解释下吗
-
richTextBox1.ScrollToCaret();
使用ScrollToCaret()可以将滚动条定位到当前光标处
richTextBox1.AppendText(DateTime.Now.Ticks.ToString()+Environment.NewLine);
richTextBox1.ScrollToCaret();richtextbox 使用ScrollToCaret()可以将滚动条定位到当前光标处
http://blog.csdn.net/keenweiwei/article/details/7305847
或是看MSDN的解释
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms