简体   繁体   English

异常System.Reflection.TargetInvocationException - Backgroundworker

[英]Exception System.Reflection.TargetInvocationException - Backgroundworker

I'm trying to read files which were opened by the openFileDialog into a richTextBox (called websiteInput_rtxt) using a backgroundworker(bgFileOpener). 我正在尝试使用backgroundworker(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());
    }

When the form is closed while the bgWorker is still running an exception is thrown which doesn't seem to be caught , can someone tell me what is missing or what could cause the exception? 当表格在bgWorker仍在运行时关闭时,会抛出一个似乎没有被捕获的异常 ,有人可以告诉我缺少什么或可能导致异常的原因吗?

Exception message is called "System.Reflection.TargetInvocationException" and the innerException says something about the RichTextBox. 异常消息称为“System.Reflection.TargetInvocationException”,而innerException则表示有关RichTextBox的内容。

Closing the form doesn't immediately stop the background worker, which means your ProgressChanged event can still be raised on the form after it's been closed. 关闭表单不会立即停止后台工作程序,这意味着您的ProgressChanged事件仍然可以在窗体关闭后在表单上引发。

You can work around this via: 你可以解决这个问题:

private void bgFileOpener_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    if (this.IsDisposed) // Don't do this if we've been closed already
    {
        // Kill the bg work:
        bgFileOpener.CancelAsync();
    }
    else
        websiteInput_rtxt.AppendText(e.UserState.ToString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Reflection.TargetInvocationException - System.Reflection.TargetInvocationException 异常信息:System.Reflection.TargetInvocationException - Exception Info: System.Reflection.TargetInvocationException 构造函数中的 System.Reflection.TargetInvocationException - System.Reflection.TargetInvocationException in constructor system.reflection.targetinvocationException SQLite - system.reflection.targetinvocationexception SQLite mscorlib:System.reflection.TargetInvocationException - mscorlib:System.reflection.TargetInvocationException System.Reflection.TargetInvocationException: '调用的目标已抛出异常 - System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation scaffold-DbContext ,命令导致异常:System.Reflection.TargetInvocationException - scaffold-DbContext , command results in exception: System.Reflection.TargetInvocationException System.Reflection.TargetInvocationException :调用的目标已抛出异常 - System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException:调用的目标已引发异常 - System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation MessagingCenter.Send 上的 System.Reflection.TargetInvocationException - System.Reflection.TargetInvocationException on MessagingCenter.Send
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM