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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:03:03+00:00 2026-06-06T04:03:03+00:00

I have a form. In that form I create an instance of a class

  • 0

I have a form.

In that form I create an instance of a class on a new thread because it runs some long running logic. The form also gives the user the ability to cancel this logic/thread.

That class opens a new form if input is required.

The new form sometimes appears behind the other form.

I set a property on the class:

public Form ParentForm{get;set;}

I can now do:

MyForm form = new MyForm();
form.ShowDialog(ParentForm);

However I get a cross thread exception when calling ShowDialog(ParentForm).

I know I can use InvokeRequired somehow but not sure how on a property.

Thanks

UPDATE: Have tried doing this but still get exception:

MyForm form = new MyForm();
form.ShowDialog(GetParentForm()); 



private Form GetParentForm()
{
    //You have to Invoke() so you can wait for the function to return and obtain its return value.
    if (ParentForm.InvokeRequired)
    {
        return (Form)ParentForm.Invoke(new Func<Form>(() => GetParentForm()));
    }
    else
    {
        return ParentForm;
    } 
}
  • 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-06T04:03:04+00:00Added an answer on June 6, 2026 at 4:03 am

    Your updated method (GetParentForm) won’t work because you’re wrapping the task of getting the reference to ParentForm in an InvokeRequired block. You could try wrapping the ShowDialog call in such a block instead, but I think you would still get the cross-threading error.

    Your simplest fix would be to move the code that creates and shows the second form out of your class and into ParentForm. So instead of this:

    MyForm form = new MyForm();
    form.ShowDialog(ParentForm);
    

    you would do this:

    ParentForm.showMyNewForm();
    

    and in ParentForm you would have this:

    public void showMyNewForm()
    {
        MyForm form = new MyForm();
        form.ShowDialog(this);
    }
    

    If MyForm needs to have a reference to the class on the other thread, you would just add a parameter to showMyNewForm() so that the reference to it can be passed in.

    What you’re trying to do here (creating and showing related, connected forms that are created on different threads) is really going against the grain of how forms are meant to be used in .NET.

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

Sidebar

Related Questions

I have a class that implements the INotifyPropertyChanged interface. If I create a new
I have an admin form that lets users create entities that require an image.
I have a web form that uses if statements to create a string of
I have a create action for a form that potentially generates errors (i.e. first
I'm have a fair complex Form that I am trying to create in Play
i have asked to create a module that will record video messages form the
I have a create action in the controller that I use for a form
So I have this JS form that creates a new record ( MySQL ,
I have a modelform that will either create a new model or edit an
Is possible create (register) a new class in runtime using delphi. I have a

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.