views:

21

answers:

2

Hi,

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  A: 

No. You need to call Application.Run, and then schedule delegates on the UI thread.

P.S. It's a better idea to use the Task class rather than Control.Invoke.

Stephen Cleary
I didn't know there is a Task class. I tend to rely on using Thread, , ThreadPool or invoke. Thanks for the info.I was hoping to make the Main() code execution "appears" to be running linearly, which would make it easier to follow when doing code review. This would hide some of the complicities when the application initializes.
dsum
A: 

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();

}
Josh Einstein
Thanks for the example. I was curious about how other people approach to the splash screen problem. I am guessing ultimately it is best to free the Main (UI) Thread asap so that other threads can interact with the UI though the invoke().
dsum
No problem. If this answered your question, you should mark it as answered by clicking the check mark.
Josh Einstein