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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:30:48+00:00 2026-05-22T21:30:48+00:00

I have some code that takes a form object (winforms) and calls form.Show() on

  • 0

I have some code that takes a form object (winforms) and calls form.Show() on a dedicated thread (it spins a new thread).
This worked fine in Visual Studio 2008 (framework 3.5). I now migrated to 2010 and it fails with
InvalodOperationException: “Cross-thread operation not valid: Control ” accessed from a thread other than the thread it was created on.”

(of course half our files are checked out for the migration and the whole team is waiting…)

Here is some code (this is the running code so fogive me for some redundant level of detail):

    private void ShowForm(object container)
    {
        FormContainer formContainer = (FormContainer)container;
        Process sboProcess = GetSboProcess();
        if (sboProcess != null)
        {
            WindowWrapper MyWindow = new WindowWrapper(sboProcess.MainWindowHandle);
            if (formContainer.ShowDialog)
            {
                formContainer.Form.ShowDialog(MyWindow);
            }
            else
            {
                //formContainer.Form.Invoke((MethodInvoker)delegate() { formContainer.Form.Show(MyWindow); });
                formContainer.Form.Show(MyWindow);
                //Run thread while form is opened:
                System.Windows.Forms.Application.Run(formContainer.Form);
            }
        }
    }



public class FormContainer
{
    private readonly Form form;
    private readonly bool showDialog;

    public FormContainer(Form form, bool showDialog)
    {
        this.form = form;
        this.showDialog = showDialog;
    }

    public bool ShowDialog
    {
        get { return showDialog; }
    }

    public Form Form
    {
        get { return form; }
    }

}

 public class WindowWrapper : IWin32Window
{
    private readonly IntPtr handle;

    public WindowWrapper(IntPtr handle)
    {
        this.handle = handle;
    }

    #region Implementation of IWin32Window

    /// <summary>
    ///                     Gets the handle to the window represented by the implementer.
    /// </summary>
    /// <returns>
    ///                     A handle to the window represented by the implementer.
    /// </returns>
    /// <filterpriority>1</filterpriority>
    public IntPtr Handle
    {
        get { return handle; }
    }

    #endregion
}

Anyone with an idea?

Thanks,

Asher

  • 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-22T21:30:48+00:00Added an answer on May 22, 2026 at 9:30 pm

    In a sentence; the dedicated thread must call back to the UI thread that owns the form. Cross-thread UI operations are illegal, because there are certain operations that could cause the UI control involved to become detached from the Windows message pump. This is a bad thing because then Windows cannot tell the window to do anything, including draw itself, move, or even close. The window becomes “rogue”, and all Windows can do is terminate the entire process that spawned the window.

    The invocation of the form method can be a synchronous or asynchronous operation, but either way it has to be queued up to be run by the UI thread.

    Here’s a way to make calls from background threads SEEM like they’re executed by the background thread, without violating cross-threading rules:

    //in your form class
    public new void Show()
    {
       if(!InvokeRequired)
          base.Show();
       else
          this.Invoke((MethodInvoker)(()=>base.Show()));
    }
    

    Understand that you cannot deal with the form as its base class System.Windows.Form; it has to be the concrete form class you create. Otherwise the method hiding is ignored and the base implementation is used by default.

    By setting it up this way, whenever an action might be about to run from a background thread, you avoid cross-threading operations. You can change this method to run asynchronously if desired by calling BeginInvoke() on the control instead of Invoke(), but as a call to Show() would normally run synchronously, I would stick with Invoke.

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

Sidebar

Related Questions

I have some code that effectively does this : File file = new File(C:\\Program
So I have some code here that takes user input from a standard web
I have some jQuery AJAX code that takes the contents of a text-area and
Does anyone have some code that will take a TimeZoneInfo field from .NET and
I have to take over and improve/finish some code that transforms Java objects from
We have taken over some .NET 1.1 Windows Service code that spawns threads to
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int

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.