Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple UI Threads

Skip to the bottom for the question; this is just some extra info

I am using a component (GeckoFX) to render some websites, well fine, yet it can only be used in a Windows Form; as it has to bind to a WinForms object that can be drawn. Because all the WinForms are running in the same thread, I can only use one GeckoFX instance at a time; so I decided to create a 'worker class' in the form of a WinForm, and add all the logic in there. The form doesn't require to communicate with the main form.

Now I can fire up 10 windows, and they will eventually work, but every new form will wait before all the other forms have handled all their GeckoFX events, as you cannot use multiple instances on one thread. Furthermore, the browser has to be on a UIThread. So:

Is it possible to create multiple UI Threads (one for each form)?

I have seen someone doing it ([edit: removed 'bad' link]), yet no one ever got his code samples working. The guy who got it working originally used some form of custom message pumping to do this kind of things, but I have no clue how to achieve something like that.

like image 318
Jan Jongboom Avatar asked Oct 14 '09 14:10

Jan Jongboom


People also ask

What is the purpose of a UI thread?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.

Is WPF multi threaded?

WPF supports a Single Threaded Apartment Model(STA) like the windows forms which introduces following constraints in a WPF application. The thread that creates a WPF UI element owns the elements and other threads can not interact with the UI elements directly,this is known as thread affinity.

What is UI thread in C#?

A UI thread creates UI elements and waits and responds to events like mouse clicks and key presses. You can only access the UI elements from the UI thread. There are two types of threads: background and foreground. A UI thread is an example of a foreground thread.

Is iOS single thread?

Each process (application) in OS X or iOS is made up of one or more threads, each of which represents a single path of execution through the application's code. Every application starts with a single thread, which runs the application's main function.


1 Answers

I don't think that what you ask is really what you want but creating a message pump per thread is easy, you just have to call Application.Run once per thread.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Thread t1 = new Thread(Main_);
        Thread t2 = new Thread(Main_);

        t1.Start();
        t2.Start();

        t1.Join();
        t2.Join();
    }

    static void Main_()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
like image 50
Julien Roncaglia Avatar answered Nov 08 '22 20:11

Julien Roncaglia