1

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;
            }

        }

そして、backgroundworkerの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)
        {


        }
4

1 に答える 1

2

フォームが破棄されているようです。その後、BackgroundWorkerは、破棄されたフォームに属していたコントロールを更新しようとしています。

フォームのFormClosingイベントを処理してワーカーを停止するか、フォームが完了するのを待つことができます。

または、BackgroundWorkerはIsDisposed、ステータスを書き出す前にフォームのプロパティを確認することもできます。

ReportProgressしかし、実際には、-ではなく、を使用する必要がありますInvoke。これにより、問題も回避されます。

于 2012-09-18T05:37:11.750 に答える