当关闭应用程序时,它的工作有时会因为不释放对象而异常吗?

时间:2012-09-18 05:29:54

标签: c#

发现了ObjectDisposedException。

无法访问已处置的对象。 对象名称:'Form1'。

当backgorundworker正在工作时,我点击右上角的X红色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;
            }

        }

后台工作者DoWork和ProgressReport事件:

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)
        {


        }

1 个答案:

答案 0 :(得分:2)

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

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

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

但实际上,你应该使用ReportProgress而不是Invoke - 这也可以避免这个问题。