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

  • Home
  • SEARCH
  • 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 935301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:08:06+00:00 2026-05-15T21:08:06+00:00

I have a function which adds a control to a parent control which is

  • 0

I have a function which adds a control to a parent control which is called from threads different to the thread the controls were created on. This is how it goes:

1        delegate void AddControlToParentDelegate(Control child, Control parent);
2        private void addControlToParent(Control child, Control parent) {
3        if (parent.InvokeRequired) {
4            AddControlToParentDelegate d = new  AddControlToParentDelegate(addControlToParent);
5            this.Invoke(d, new object[] { child, parent });
6            } else {
7                parent.Controls.Add(child);
8            }
9        }
10    }

This works fine up until both parent.InvokeRequired as well as child.InvokeRequired are TRUE. Then, once line 5 is executed (now the delegate d is invoked and the function is supposed to be running on the UI thread. (right?)) child on line 7 throws a cross-thread operation invalid exception. Why is this? Isn’t it already running on the thread it was created?

I managed to fix this by adding an extra (child.InvokeRequired) check:

delegate void AddControlToParentDelegate(Control child, Control parent);
private void addControlToParent(Control child, Control parent) {
    if (parent.InvokeRequired) {
        AddControlToParentDelegate d = new AddControlToParentDelegate(addControlToParent);
        this.Invoke(d, new object[] { child, parent });
    } else {
        if (child.InvokeRequired) {
            this.Invoke(new MethodInvoker(delegate() {
            parent.Controls.Add(child);
            }));
        } else {
            parent.Controls.Add(child);
        }
    }
}

but this just seems terribly hack-y and unnecessary. Is this the way to do it? Or have I totally missed the bus?

  • 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-15T21:08:07+00:00Added an answer on May 15, 2026 at 9:08 pm

    Note that InvokeRequired is unreliable when the control has no window handle. This will almost certainly be the case for a newly created child control that does not have a parent yet, which appears to be the case in your “fixed” code.

    See Ivan Krivyakov’s detailed analysis: http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html

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

Sidebar

Related Questions

Hey I have a function which is called from a html link like this:
We have an immediate function in the file 'mediator.js' which adds a 'mediator' object
hi i have function which is called by tinker listbox so i cannot return
I have a function which looks something like this, it returns a noncopyable class
I have written the following function which adds an event listener and registers the
Please help me, i'm stuck at this. I have a function which must sum
I have a function which adds a character to a string: void AddChToString(char **str,char
I have made a form which adds controls dynamically with user selecting the type
Alright, I have jQUery function which adds class to a div#float once div#floatLunch gets
I have a function in jQuery which adds to my jsp view a form

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.