我正在尝试用背景工作者(bgFileOpener)将由 openFileDialog 打开的文件读入到一个 richTextBox (称为 websiteInput_rtxt)中。
当表单关闭时,如果bgWorker仍在运行,则会抛出异常,似乎没有被捕获。请问有什么遗漏或可能导致异常的原因吗?
异常消息称为“System.Reflection.TargetInvocationException”,内部异常显示与RichTextBox有关。
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”,内部异常显示与RichTextBox有关。
websiteInput_rtxt.AppendText(e.UserState.ToString());
是否在工作线程上运行? - Arian Motamedi