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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:34:01+00:00 2026-06-07T07:34:01+00:00

Because this is turning out to be quite a large project and I often

  • 0

Because this is turning out to be quite a large project and I often need to access objects in other forms from a class I firstly create a static variable in my program.cs class of type ‘MainForm’which is a Windows Form.

From my DataManger.cs Class, I instantiate the object ‘Program.MainFormObj = new MainForm()’ and then call the Show method in order to display my Form. It looked like the the Paint event for the form doesn’t get to finish but it does kick off as some controls are drawn. Presumably because the main thread (this is all running on one the main thread) is ‘occupied’ by the next statement. I figured, regardless of known problems I’d use Application.DoEvents() to temporarily counter this issue. As I imagined, it worked, but only to a certain extent. If I put a blocking method like MessageBox.Show(), I can view my completely flawless form in the background but as soon as I press ‘OK’ and the code proceeds the entire application exits.

I’m really stuck. Despite some extensive searching both here and using Google I can’t find an apporpiate answer. I did this this thread: Form.Show() is not showing it's child controls but the given solution isn’t appropriate for my solution. Any help is appreciated very much, thanks.

  • 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-07T07:34:03+00:00Added an answer on June 7, 2026 at 7:34 am

    If this is a large project, invest time now to refactor it.

    • Consider an MVC pattern. You can do what with WinForms, not just newer UI technologies.
    • If you do not opt for WinForms, at least separate UI and data (Data Binding is your friend).
    • Build reusable components when different forms share common functionality.

    Fighting the way WinForms works will be much more expensive in the mid-term than investing time now to do things right.

    If you have any choice about the technology, consider WPF instead. There is a learning curve with WPF, but it may well be worth investing in that learning now.

    UPDATE

    Based on your code sample, I see that you are probably creating some of your forms on a thread other than the main UI thread. While that is allowed, it creates for very confusing code because forms can only be updated from the thread they were created on.

    The simplest hack to get this working is probably to update HandleIncomingData to also invoke the Show() calls on the main UI thread.

    There is supposed to be a way to cleanly do that from a static method, outlined here:

    https://stackoverflow.com/a/505361/141172

    However, when I tried that, I found that SynchronizationContext was always null. I’m sure I’m just missing a step.

    I was able to get things working by misusing the LoginForm (assuming, perhaps incorrectly, that the form is always available. If not, perhaps create a special form just for thread marshaling).

    The hack looks like this:

    Program.MainForm = new MainForm();
    try
    {
    
        MessageBox.Show("Login Success.", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
                // Abusing LoginForm's Invoke method.  Code assumes LoginForm is always available.
        Program.LoginForm.Invoke((MethodInvoker)delegate
        {
            Program.MainForm.Show();
        });
    
    
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.ToString()); //Debuging purposes
    }
    

    By the way, there’s an excellent extension method that makes it a breeze to invoke UI code from a non-UI thread:

    https://stackoverflow.com/a/3588137/141172

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

Sidebar

Related Questions

Because this is my first attempt at an extension method that seems quite useful
This question is mostly academic. I ask out of curiosity, not because this poses
I need this because the constructor in the superclass is calling a method which
According to the documentation for -[UIView setNeedsLayout] : Because this method does not force
hope to get some help here because this is something that really makes me
it is very strange, because this error doesn't happen all the time... I have
I'm not sure StackOverflow is the right place to ask this question, because this
I use to set WithEvents variables to Nothing in Destuctor , because this will
background: url({{ MEDIA_URL }}/bg.jpg); That does not work, because this Django template function only
I have a commit, I have stored in a branch, because this should go

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.