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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:15:45+00:00 2026-05-13T19:15:45+00:00

I am baffled by this simple task i do over and over again. I

  • 0

I am baffled by this simple task i do over and over again.

I have an array of child forms. The array is initiated in another form’s constructor:

frmChildren = new ChildGUI[20];

When the user requests to see a child form, i do this:

if (frmChildren[nb] == null)
{
    frmChildren[nb] = new ChildGUI();
    frmChildren[nb].MdiParent = this.MdiParent;
}
frmChildren[nb].Show();

So far this works. In the background i can download new content for these forms. When a download is finished i fire a ChildChange event. Here is where it stops working.
I simply want to close/hide any forms open then regenerate a new set of -frmChildren = new ChildGUI[20];- here is one of many trials:

        for (int i = 0; i < frmChildren.Length;i++ )
        {
            if (frmChildren[i] != null)
            {
                //frmChildren[i].BeginInvoke(new EventHandler(delegate
                //{
                    frmChildren[i].Close();
                //}));
            }
        }             
        frmChildren= new ChildGUI[20];

I get a Cross Thread exception on the .Close(). Notice i’ve already tried doing an invoke, but doing so bypasses the !=null for some reason. I think it may have something to do with the garbage collector. Anybody have an input?

  • 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-13T19:15:46+00:00Added an answer on May 13, 2026 at 7:15 pm

    The problem is that your anonymous method is capturing i – so by the time it’s actually invoked in the UI thread, you’ve got a different value of i, which may be null. Try this:

    for (int i = 0; i < frmChildren.Length; i++)
    {
        ChildGUI control = frmChildren[i];
        if (control != null)
        {
            control.BeginInvoke(new EventHandler(delegate
            {
                control.Close();
            }));
        }
    }             
    frmChildren = new ChildGUI[20];
    

    See Eric Lippert’s blog post for why introducing a new variable within the loop fixes the problem.

    EDIT: If you want to use a foreach loop, it would look like this:

    foreach (ChildGUI control in frmChildren)
    {
        // Create a "new" variable to be captured
        ChildGUI copy = control;
        if (copy != null)
        {
            copy.BeginInvoke(new EventHandler(delegate
            {
                copy.Close();
            }));
        }
    }             
    frmChildren = new ChildGUI[20];
    

    Just as an aside, you can use the fact that you just want to call a void method to make the code slightly simpler. As this no longer uses an anonymous method, you can make do away with the “inner” variable:

    foreach (ChildGUI control in frmChildren)
    {
        if (control != null)
        {
            control.BeginInvoke(new MethodInvoker(control.Close));
        }
    }             
    frmChildren = new ChildGUI[20];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.