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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:12:50+00:00 2026-05-15T23:12:50+00:00

I have a situation where I would like to have the main thread waiting,

  • 0

I have a situation where I would like to have the main thread waiting, while other threads can invoke on the main thread, without calling Application.Run.

The following code shows what I try to achieve, except that the main thread and the data loading thread are causing a dead lock.

    static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var form1 = new Form1();
        form1.Show();

        Thread loadDataThread = new Thread(() =>
        {
            //Load Data that takes a long time
            string title = LoadData();
            form1.Invoke((Action)delegate { form1.Text = title; });
        });

        loadDataThread.Name = "Data Loading Thread";
        loadDataThread.Start();

        //wait for loading to be completed (deadlock)
        loadDataThread.Join();

        //Do more stuffs

        Application.Run();
    }

    private static string LoadData()
    {
        Thread.Sleep(1000);
        return "Hello Thread";
    }
}

Thank you

  • 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-15T23:12:51+00:00Added an answer on May 15, 2026 at 11:12 pm

    Invoke only works because in a Windows Forms application there is a message loop running for the entire lifetime of the application. If you are blocked on Thread.Join, then the message loop is not processing messages which also means it’s not processing your Invoke calls.

    There are some blocking calls in the .NET framework that do pump messages but you can’t (and shouldn’t) rely on these because more often than not they create re-entrancy bugs in your application that are a huge pain to resolve.

    Instead of blocking the UI thread while you’re waiting for data to load, you should just let it run in the background with your splash screen showing. Then at the end you can use BeginInvoke to close the splash screen and open the main screen or whatever it is you need to do at the end of the loading. Something like…

    static void Main() {
    
        Application.EnableVisualStyles();
    
        var splashScreen = new Form1();
        var mainScreen = new Form2();
    
        splashScreen.Show();
    
        ThreadPool.QueueUserWorkItem(delegate {
    
            LoadLotsOfData();
    
            splashScreen.BeginInvoke(delegate {
                splashScreen.Close();
                mainScreen.Show();
            });
    
        }
    
        Application.Run();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following situation. A main table and many other tables linked together with
I've got a MVVM-like situation (not using any frameworks) where I have a main
I have the following situation that I would like to handle in JSF. My
I have this situation, where I would like to display a number in figures.
I have the following situation I think would be best to show in sample
I have a situation in which the ideal relationship, I believe, would involve Value
How would I setup my controls for the following situation?: I have a parent-container
I am curious how others would handle this situation. I have a domain layer
Here is the situation, I have UIViewController class with a UITableView outlet. I would
i have situation like this: class IData { virtual void get() = 0; virtual

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.