我正在尝试使用后台工作程序(bgFileOpener)将 openFileDialog 打开的文件读入richTextBox(称为 websiteInput_rtxt)。
private void bgFileOpener_DoWork(object sender, DoWorkEventArgs e)
{
try
{
foreach (var file in openFileDialog1.FileNames)
{
using (StreamReader sreader = new StreamReader(file))
{
// while the stream reader didn't reach the end of the file - read the next line and report it
while (!sreader.EndOfStream)
{
if (bgFileOpener.CancellationPending)
{
e.Cancel = true;
return;
}
bgFileOpener.ReportProgress(0, sreader.ReadLine() + "\n");
Thread.Sleep(15);
}
}
}
}
catch (Exception) { }
}
private void bgFileOpener_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
websiteInput_rtxt.AppendText(e.UserState.ToString());
}
当表单在 bgWorker 仍在运行时关闭时,抛出似乎没有被捕获的异常,有人可以告诉我缺少什么或什么可能导致异常吗?
异常消息称为“System.Reflection.TargetInvocationException”,innerException 说明了有关 RichTextBox 的信息。