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 1055419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:33:34+00:00 2026-05-16T17:33:34+00:00

I am loading a big treeview in a seperate thread. This thread starts at

  • 0

I am loading a big treeview in a seperate thread. This thread starts at the load event of a form.

All goes well, until an error occurs in the load event. When an error occurs I close the form and the thread that loads my treeview must be aborted. But I don’t know how to do this.

The problem is, that the form is closed and the thread is still working, so I get an InvalidOperationException. The program breaks down and this part of the thread is highlighted:

tvQuestionnaire.Invoke((MethodInvoker)delegate
{
    tvQuestionnaire.Nodes.Add(catNode);
});

The treeview on my form is called tvQuestionnaire. The whole function (which is called in my background worker) looks like this:

private void SetTreeviewData()
{
    // Get all categories
    List<Category> categories = _questionnaire.GetCategoriesFromQuestionnaire();

    // Get all questions which are retrieved by the question manager
    OrderedDictionary all_ordered_questions = _questionManager.AllQuestions;

    // Store all the questions in a List<T>
    List<Question> all_questions = new List<Question>();
    foreach (DictionaryEntry de in all_ordered_questions)
    {
        Question q = de.Value as Question;
        all_questions.Add(q);
    }

    foreach (Category category in categories)
    {
        // Create category node
        TreeNode catNode = new TreeNode();
        catNode.Text = category.Description;
        catNode.Tag = category;
        catNode.Name = category.Id.ToString();

        // Get all questions which belongs to the category
        List<Question> questions = all_questions.FindAll(q => q.CategoryId == category.Id);

        // Default set the font to bold (Windows issue)
        Font font = new Font(tvQuestionnaire.Font, FontStyle.Regular);

        foreach (Question question in questions)
        {
            // Create question node
            TreeNode queNode = new TreeNode();
            queNode.Text = question.Question;
            queNode.Tag = question;
            queNode.Name = "Q" + question.Id;
            queNode.NodeFont = font;

            // Determine which treenode icon to show
            SetTreeNodeIcon(ref queNode, question);

            // Add node to category node
            catNode.Nodes.Add(queNode);
        }

        if (_closing)
            return;

        // Add category node to treeview
        tvQuestionnaire.Invoke((MethodInvoker)delegate
        {
            tvQuestionnaire.Nodes.Add(catNode);

            // Now the category (and thus the questions) are added to treeview
            // Set questions treenode icon
            //SetTreeNodeIcon(questions);
        });
    }

    // Set each category under its parent
    for (int i = tvQuestionnaire.Nodes.Count - 1; i >= 0; i--)
    {
        Category category = tvQuestionnaire.Nodes[i].Tag as Category;
        TreeNode node = tvQuestionnaire.Nodes[i];

        if (IsWindow(this.Handle.ToInt32()) == 0)
            return;

        tvQuestionnaire.Invoke((MethodInvoker)delegate 
        {
            if (category.ParentId == null)
                return;
            else
            {
                // Find parent node
                TreeNode[] parentNodes = tvQuestionnaire.Nodes.Find(category.ParentId.ToString(), true);

                //Remove current node from treeview
                tvQuestionnaire.Nodes.Remove(node);
                parentNodes[0].Nodes.Insert(0, node);
            }
        });
    }
}

This is the only method that my background worker calls.

So my question is, how can I prevent that the Exception occurs? How do I check the form where the treeview is on, is still ‘alive’?

  • 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-05-16T17:33:35+00:00Added an answer on May 16, 2026 at 5:33 pm

    One solution would be to call the CancelAsync method of the backgroundworker (BGW) when you need to close the form. In the DoWork event handler, check at the beginning of the loop that cancellation has not been requested. If it was, exit the loop (and the DoWork handler).
    In the form, wait for the BGW to complete (either success or cancellation)

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

Sidebar

Related Questions

While loading a Windows Form (C#), I'm trying to iterate through all controls. So
After loading a PHP template (using jQuery's load function), this simple script won't make
What are some tips of loading big image faster. I look at this website:
I have the following void: public void load() { //loading big picture from the
How to show loading image when a big image is being loaded? As an
I'm wondering if there is a big difference when loading graphics made in pure
Error loading script occurs under FireFox 3 if I quickly click different links on
I have a big file full of integers that I'm loading in. I've just
I'm loading data using dojo.io.script.get. Size of each request can be big and I
My landing page contains a big google maps portion, which slows down the loading

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.