Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当关闭应用程序时,有时我会因为没有释放对象而收到异常,为什么?_C# - Fatal编程技术网

C# 当关闭应用程序时,有时我会因为没有释放对象而收到异常,为什么?

C# 当关闭应用程序时,有时我会因为没有释放对象而收到异常,为什么?,c#,C#,已捕获ObjectDisposedException 无法访问已释放的对象。 对象名称:“Form1” 这是当ReGrOnWork工作时,我点击右上角的X Red X来关闭应用程序,当它在工作中间时,有时会抛出这个异常。 System.ObjectDisposedException was caught Message=Cannot access a disposed object. Object name: 'Form1'. Source=System.Windows.Forms

已捕获ObjectDisposedException

无法访问已释放的对象。 对象名称:“Form1”

这是当ReGrOnWork工作时,我点击右上角的X Red X来关闭应用程序,当它在工作中间时,有时会抛出这个异常。

System.ObjectDisposedException was caught
  Message=Cannot access a disposed object.
Object name: 'Form1'.
  Source=System.Windows.Forms
  ObjectName=Form1
  StackTrace:
       at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
       at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
       at System.Windows.Forms.Control.Invoke(Delegate method)
       at GatherLinks.Form1.test(String url, Int32 levels, DoWorkEventArgs eve) in D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 126
  InnerException: 
在这种情况下,第126行是:

this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));
我应该如何修复/修复此异常,以及在退出应用程序时应该释放/关闭哪些对象或变量

测试功能:

private List<string> test(string url, int levels,DoWorkEventArgs eve)
        {
            levels = levelsToCrawl;
            HtmlWeb hw = new HtmlWeb();
            List<string> webSites;
            List<string> csFiles = new List<string>();

            csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
            csFiles.Add("current site name in this level is : " + url);

            try
            {
                this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, "Loading The Url:   " + url + "..." , Color.Red); }));
                HtmlAgilityPack.HtmlDocument doc = GetHtmlDoc(url, reqOptions, null);
                if (timeOut == true)
                {
                    this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " There Was A TimeOut" + Environment.NewLine , Color.Red); }));
                    timeOut = false;
                    return csFiles;
                }
                else
                {
                    this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));
                }
                currentCrawlingSite.Add(url);
                webSites = getLinks(doc);
                removeDupes(webSites);
                removeDuplicates(webSites, currentCrawlingSite);
                removeDuplicates(webSites, sitesToCrawl);
                if (removeExt == true)
                {
                    removeExternals(webSites);
                }
                if (downLoadImages == true)
                {
                    retrieveImages(url);                 }

                if (levels > 0)
                    sitesToCrawl.AddRange(webSites
                this.Invoke(new MethodInvoker(delegate { label7.Text = sitesToCrawl.Count.ToString(); }));
                this.Invoke(new MethodInvoker(delegate { label3.Text = currentCrawlingSite.Count().ToString(); }));



                if (levels == 0)
                {
                    return csFiles;
                }
                else
                {


                    for (int i = 0; i < webSites.Count(); i++)//&& i < 20; i++)                     {
                        int mx = Math.Min(webSites.Count(), 20); 


                            string t = webSites[i];
                            if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) 
                            {

                                csFiles.AddRange(test(t, levels - 1, eve));
                            }

                    }
                    return csFiles;
                }



            }
            catch
            {
                return csFiles;
            }

        }

看起来您的表单正在被处置,然后您的BackgroundWorker正在尝试更新属于已处置表单的控件

您可以处理表单的
FormClosing
事件并停止工作程序,或者等待它完成

或者,您的BackgroundWorker可以在尝试写出状态之前检查表单的
IsDisposed
属性


但实际上,您应该使用,而不是
Invoke
,这样也可以避免问题。

如果没有关于触发该代码原因的上下文,几乎可以肯定的是,表单
Form1
已被释放,然后您的事件被调用。我如何使用reportprogress执行该操作可能存在重复?测试函数是一个一直在工作的函数,可以调用它自己。在backgroundworker DoWork事件中,我第一次在CE上调用这个测试函数。然后函数测试一直在调用self。在backgroundworker ReportProgress事件中,我现在没有做任何事情。因此,我需要使用某种方式从测试函数报告,并在reportprogress事件中执行某些操作。我该怎么做?
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

                this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
                this.Invoke(new MethodInvoker(delegate { label4.Text = mainUrl; }));
                test(mainUrl, levelsToCrawl, e);


        }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {


        }