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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:24:08+00:00 2026-05-24T17:24:08+00:00

I have a winforms app, and I need to access the Handle property of

  • 0

I have a winforms app, and I need to access the Handle property of a main form, inside a Backgroundworker thread.

I have made a thread safe method that calls the main form with InvokeRequired. My question is – why do I still get “InvalidOperationException cross-thread operation not valid” error, even when calling this thread safe method like this:

ProcessStartInfo psi = new ProcessStartInfo(file);
psi.ErrorDialogParentHandle = Utils.GetMainAppFormThreadSafe().Handle;

And below is the code of the thread safe method (my main app form is called Updater):

    /// <summary>
    /// delegate used to retrieve the main app form
    /// </summary>
    /// <returns></returns>
    private delegate Updater delegateGetMainForm();

    /// <summary>
    /// gets the mainform thread safe, to avoid cross-thread exception
    /// </summary>
    /// <returns></returns>
    public static Updater GetMainAppFormThreadSafe()
    {
        Updater updaterObj = null;
        if (GetMainAppForm().InvokeRequired)
        {
            delegateGetMainForm deleg = new delegateGetMainForm(GetMainAppForm);
            updaterObj = GetMainAppForm().Invoke(deleg) as Updater;
        }
        else
        {
            updaterObj = GetMainAppForm();
        }
        return updaterObj;
    }

    /// <summary>
    /// retrieves the main form of the application
    /// </summary>
    /// <returns></returns>
    public static Updater GetMainAppForm()
    {
        Updater mainForm = System.Windows.Forms.Application.OpenForms[Utils.AppName] as Updater;
        return mainForm;
    }

Am I doing smth wrong?
Thank you in advance.

LATER EDIT: I’ll post the reason why I need the handle in the first place, perhaps there is another solution/approach. In My Backgroundworker thread I need to install multiple programs in a loop, and I start a process for each installer. However I need to ask for elevation so that this operation can work for standard users as well, not only admins. In short, I am trying to follow the tutorial here

  • 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-24T17:24:10+00:00Added an answer on May 24, 2026 at 5:24 pm

    You aren’t getting the handle in a thread-safe way. Instead you get the Form instance in a thread-safe way and then access the Handle property in an unsafe way.

    You should add a method GetMainAppFormHandle() that directly returns the handle and call that one in a thread-safe way:

    public static IntPtr GetMainAppFormHandle()
    {
        return System.Windows.Forms.Application.OpenForms[Utils.AppName].Handle;
    }
    

    Update:

    In addition you need GetMainAppFormHandleThreadSafe() instead of GetMainAppFormThreadSafe():

    public static IntPtr GetMainAppFormHandleThreadSafe()
    {
        Form form = GetMainAppForm();
        if (form.InvokeRequired)
        {
            return (IntPtr)form.Invoke(new Func<IntPtr>(GetMainAppFormHandle));
        }
        else
        {
            return GetMainAppFormHandle();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a winforms app and the main (and only) form has several buttons.
I have a ListBox in my winforms app and I need to handle the
I've got a VB.NET WinForms app in which I have the need to refer
I have a simple winforms app with one form, a few controls and a
I have WinForms app and multiple forms and want to use ErrorProvider component on
I have a WinForms app written in C# with .NET 3.5. It runs a
I have a winforms app and i want to keep track of every time
I have a Winforms app that has a DataGridView that is databound at runtime.
I have a winforms app, that's locking up during a web service request I've
I have a winforms app that checks user credentials as it starts. If autharization

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.