views:

2436

answers:

7

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();
     }
    }
}
A: 

t.Join() ?

plinth
If I put that on the main thread it will join immediately, if I put that on the UI Thread it will block disallowing user interaction.
George Mauer
Ah yes - I missed that you were doing form.Show() not Application.Run in your thread.
plinth
+6  A: 

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);
    }
}
Lasse V. Karlsen
ahh...I think Application.Run might be my missing jigsaw piece
George Mauer
+2  A: 

You cannot open a GUI form in any thread, because it will be missing a message pump. You have to explicitly start a message pump in that thread by invoking Application.Run() in a thread method. Another option is to call a DoEvents() in a loop, if you need to do something else, because after Application.Run() that thread will wait a user to close a form in that point of execution.

Nenad
A: 

private void button1_Click(object sender, EventArgs e) { var t = new Thread(RunNewForm); t.Start(); } public static void RunNewForm() { Application.Run(new Form2()); }

CheeZe5
A: 

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.

rice
A: 

I think just calling ShowDialog instead of Show will help. The problem seems to be that the thread finishes just after calling Show, after that the Form get's garbage collected. ShowDialog will halt the thread but still run form-events on it so the thread will keep running until the form is closed.

Normally i would do it the other way around. Run the form on the starting thread and start background threads when you want to start long-running background tasks.

I also read your other question but couldn't figure out what you're trying to do. MVP-architecture doesn't require you to run your business logic on different threads. Multi threading is hard to do right so I'd only use multiple threads if I really needed them.

Mendelt
A: 

Thank you for the answer from Lasse V. Karlsen, I used the code and runs OK, really thanks very much!!!

Sherlock Yang