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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:25:35+00:00 2026-05-31T08:25:35+00:00

Quick question: Is it possible to showDialog/Show form from a new thread that has

  • 0

Quick question: Is it possible to showDialog/Show form from a new thread that has been started from within Load event of a different form?

EDIT: Why is it not possible to showDialog/Show form from a new thread that has been started from within Load event of a different form?

The supposed requirement is to show a loader form /spiral thingy with loading text, and possibly more/ while MDI child form is loading thus hanging the whole application.

The requirement states that the “Loader form” must not hang. Thus it requires a new thread.

code summary, that I have implemented so far:
MDI Parent:

delegate void ManipulateJob();
public void StartJob()
{
    Cursor.Current = Cursors.WaitCursor;

    if (this.InvokeRequired) 
    //case when the Loader is started from a different thread than the main we need to invoke
    {
        System.Diagnostics.Debug.WriteLine("-> MainForm.StartJob  InvokeRequired");
        ManipulateJob callback = new ManipulateJob(StartJob);
        this.Invoke(callback, new object[] { });
    }
    else
    {
        tasks_running++;
        System.Diagnostics.Debug.WriteLine("-> MainForm.StartJob  InvokeNotRequired");

        if ((this.t == null || !this.t.IsAlive)&&tasks_running == 1)
        {
            System.Threading.ThreadStart ts = new System.Threading.ThreadStart(StartDifferent);
            this.t = new System.Threading.Thread(ts);
            this.t.Name = "UI Thread";

            System.Diagnostics.Debug.WriteLine(" **starting thread");
            this.t.Start();
            while (_form==null||!_form.IsHandleCreated) 
            //do not continue until the loader form has been shown when this is enabled
            //the whole program hangs here when StartJob is called within Load event
            {
                System.Threading.Thread.Yield();
            }
        }

    }

    System.Diagnostics.Debug.WriteLine("<- MainForm.StartJob");
}

private static frmLoading _form;
public void StartDifferent()
{
    System.Diagnostics.Debug.WriteLine(" **thread started");
    _form = new frmLoading();
    System.Diagnostics.Debug.WriteLine(" **loader created");

    _form.Icon = this.Icon;
    System.Diagnostics.Debug.WriteLine(" **loader icon set");

    _form.ShowDialog();


    System.Diagnostics.Debug.WriteLine(" **thread terminating");
}

public void StopJob()
{
    if (this.InvokeRequired) //in case this is called from a different thread
    {
        System.Diagnostics.Debug.WriteLine("-> MainForm.StopJob  InvokeRequired");
        ManipulateJob callback = new ManipulateJob(StopJob);
        this.Invoke(callback, new object[] { });
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("-> MainForm.StopJob  InvokeNotRequired");
        if (tasks_running>0&&--tasks_running == 0)
        {
            StopDifferent();
        }
    }
    System.Diagnostics.Debug.WriteLine("<- MainForm.StopJob");

    Cursor.Current = Cursors.Default;
}

delegate void CloseLoadingForm();
public void StopDifferent()
{
    System.Diagnostics.Debug.WriteLine("-> MainForm.StopDifferent");

    try
    {
        if (_form != null && _form.IsHandleCreated)
        {
            CloseLoadingForm callback = new CloseLoadingForm(_form.Close); 
            //_form itself is always on a different thread thus, invoke will always be required
            _form.Invoke(callback);
        }
    }
    finally
    {
        try
        {
            if (this.t != null && this.t.ThreadState == System.Threading.ThreadState.Running)
                this.t.Join();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("     MainForm.StopDifferent t.Join() Exception: " + ex.Message);
        }
    }
    System.Diagnostics.Debug.WriteLine("<- MainForm.StopDifferent");
}

child form example:

private void frmPricingEvaluationConfig_Load(object sender, EventArgs e)
{
    //taking out the following and putting it before 
    //Form frm = new Form();
    //frm.Show(); works
    if (this.MdiParent is IThreadedParent)
    {
        ((IThreadedParent)this.MdiParent).StopJob();
    }

    // the loader form is not displayed yet

    System.Diagnostics.Debug.WriteLine(" loading pricingEvaluationConfig");
    loadPricingClasses();
    loadEvaluationClasses();

    if (this.MdiParent is IThreadedParent)
    {
        ((IThreadedParent)this.MdiParent).StopJob();
    }
}

//loader form displays here, but is unable to close because the closing has been called

please don’t mind the namings and the fact that there are 2 matching sets of start and stop.

the question is whenever i call Startjob from within a form1_Load(object sender, EventArgs e) the _form (AKA loader) is not displayed until the Load method finishes.

EDIT:
In other words, the Loader does not display until the actual form is displayed, the only time the Loader is “Shown” (displayed on screen) is when the actual form is finished the onLoad method.
:ENDEDIT

if i take out the Startjob out of the load handler method and put it before I declare and Show() then everything works the way it supposed to

EDIT:
I assume, without any proof, that Form’s CreateHandle() method that is called within ShowDialog() and Show() methods accesses a static property somewhere checking whether any handles are already being created, and if so it stacks its own creation after the current Handle Creation, because during this whole thing somewhere the code tried to Close() the Loader/Splash and threw an error saying “Cannot Close() a form that is Creating Handle.” So I assumed the CreateHandle() method has somewhere something similar to my

while() { Thread.Yield(); } 

thus creating 2 threads /1 of which is the Loader\Splash creator thread/ which continuously Yield to one another.

Basically, I don’t understand why all of this is happening, if my assumptions are correct why should Forms should queue after one another?
:ENDEDIT

I will gladly answer all questions and concerns about the code, please ask if anything I’m doing here is vague, or you don’t quite understand the need of it.

  • 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-31T08:25:36+00:00Added an answer on May 31, 2026 at 8:25 am

    It seems like a more appropriate solution is to perform the long-running initialization task in a separate thread, and enable the MDI child window once that initialization is complete (so allow MDI child window creation to complete, but disable or keep the window invisible until a separate loader thread completes. Have your splash screen enable or make visible the child window once loading tasks are done).

    I’m not sure whether I’m following your current code correctly, but it seems that you’re blocking the OnLoad event. That will prevent the windows message queue, which in turn would block any UI rendering for the application.

    You can then use any WinForms splash screen solution until the thread performing the load completes, for example

    http://www.codeproject.com/Articles/5454/A-Pretty-Good-Splash-Screen-in-C

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

Sidebar

Related Questions

Quick question - is it possible to detect whether a focus has come from
Quick question, is it possible to have an input field in a form use
quick question : Is it possible to make a pointer that can reference a
Kind of a quick question: Is it possible to activate a viewstate from XAML?
Just a quick question for those of you that know. Is it possible to
I have a quick question that I need some help with if possible. Am
Quick question... Is is possible to attach an EventListener to a function? Such that
Just a quick question, is it possible to verify the files downloaded from FTP?
Quick question, Is it possible to have a form submitted when a radio button
I have a quick question that's been bugging me for a while. Is it

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.