Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a form in a thread and force it to stay open

I am still having problems with figuring out how to create winforms in a separate UI thread that I discussed here.

In trying to figure this out I wrote the following simple test program. I simply want it to open a form on a separate thread named "UI thread" and keep the thread running as long as the form is open while allowing the user to interact with the form (spinning is cheating). I understand why the below fails and the thread closes immediately but am not sure of what I should do to fix it.

using System;
using System.Windows.Forms;
using System.Threading;

namespace UIThreadMarshalling {
    static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var tt = new ThreadTest();
            ThreadStart ts = new ThreadStart(tt.StartUiThread);
            Thread t = new Thread(ts);
            t.Name = "UI Thread";
            t.Start();
            Thread.Sleep(new TimeSpan(0, 0, 10));
        }

    }

    public class ThreadTest {
        Form _form;
        public ThreadTest() {
        }

        public void StartUiThread() {
            _form = new Form1();
            _form.Show();
        }
    }
}
like image 432
George Mauer Avatar asked Oct 03 '08 15:10

George Mauer


2 Answers

On a new thread, call Application.Run passing the form object, this will make the thread run its own message loop while the window is open.

Then you can call .Join on that thread to make your main thread wait until the UI thread has terminated, or use a similar trick to wait for that thread to complete.

Example:

public void StartUiThread()
{
    using (Form1 _form = new Form1())
    {
        Application.Run(_form);
    }
}
like image 120
Lasse V. Karlsen Avatar answered Oct 12 '22 00:10

Lasse V. Karlsen


I think your problem is with this thought: "open a form on a separate thread named 'UI thread'"

The way windows works is like this (plz note Vista may change some of these realities):

There is one important thread called the "Main Thread" or the "UI Thread". This thread is the one that processes windows messages, like "hey the mouse clicked on this pixel."

These messages go into a queue, and the main thread processes them when it isn't busy doing something else.

So if you make a function call foo() on the main thread, if it takes a long time, no windows messages are processed during that time, and so no user interaction can occur.

The main thread also paints the UI on the screen, so long-running foo() will also stop your app from painting.

All other threads besides this holy and special main thread are grunt worker threads. These worker threads can do things, but they can never interact directly with the user interface.

This reality causes two problems:

  1. GETTING OFF THE MAIN THREAD: Since you don't want long-running foo() to halt all user interaction, you need to ship that work off to a worker thread.

  2. GETTING BACK TO THE MAIN THREAD: When long-running foo() completes, you probably want to notify the user by doing something in the UI, but you cannot do that in a worker thread, so you need to "get back" to the main thread.

So I believe your problem in the above program is very general: Your very goal is incorrect, because it is not supposed to be possible to call _form.Show() in any thread but the holy main thread.

like image 41
rice Avatar answered Oct 12 '22 00:10

rice