Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:14:36+00:00 2026-06-11T11:14:36+00:00

ObjectDisposedException was caught. Cannot access a disposed object. Object name: ‘Form1’. Thats when the

  • 0

ObjectDisposedException was caught.

Cannot access a disposed object.
Object name: ‘Form1’.

Thats when the backgorundworker is working and i click the X red X on the right top corner to close the application when its in the middle of the work sometimes its throwing this exception.

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: 

Line 126 in this case is:

this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));

What should i do to fix/repair this exception and what objects or variables should i release/close when exiting the application ?

The test function:

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

        }

And the backgroundworker DoWork and ProgressReport events:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:14:37+00:00Added an answer on June 11, 2026 at 11:14 am

    Looks like your form is being disposed, and then your BackgroundWorker is trying to update a control which belonged to the disposed form.

    You could handle your form’s FormClosing event and stop the worker, or wait for it to complete.

    Alternatively, your BackgroundWorker could check the form’s IsDisposed property before attempting to write out the status.

    But really, you should be using ReportProgress, rather than Invoke – which would avoid the problem as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting ObjectDisposedException was unhandled with message Cannot access a disposed object. This happens
I have an application that sporadically throws this exception: System.ObjectDisposedException: Cannot access a disposed
I am getting the folloinwg error from NHibernate: System.ObjectDisposedException: Session is closed! Object name:
Possible Duplicate: How does one tell if an IDisposable object reference is disposed? Is
This fails with an ObjectDisposedException when trying to assert: [Test] public void Resolve_SingletonAndDisposeChildContainer_ShouldNotDisposeSingleton() {
I use NHiberante at my win service. Sometimes I get System.ObjectDisposedException: Session is closed!
We're working with Entity Framework in a Windows Azure environment and till now we
I am working on an application that uses Crystal Reports for the reporting. It
The following exceptions are probably very similar: ArgumentException ArgumentNullException InvalidOperationException ObjectDisposedException . Should I
I have a C# application which is hitting an ObjectDisposedException with the message Safe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.