运行多个 UI 线程 [英] Run multiple UI Threads

查看:32
本文介绍了运行多个 UI 线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跳到问题底部;这只是一些额外的信息

我正在使用一个组件(GeckoFX)来渲染一些网站,很好,但它只能在 Windows 窗体中使用;因为它必须绑定到可以绘制的 WinForms 对象.因为所有的 WinForms 都在同一个线程中运行,所以我一次只能使用一个 GeckoFX 实例;所以我决定以 WinForm 的形式创建一个工人类",并在其中添加所有逻辑.该表单不需要与主表单进行通信.

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.

现在我可以启动 10 个窗口,它们最终会工作,但是每个新表单都会在所有其他表单处理它们的所有 GeckoFX 事件之前等待,因为您不能在一个线程上使用多个实例.此外,浏览器必须在 UIThread 上.所以:

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:

是否可以创建多个 UI 线程(每个表单一个)?

我见过有人这样做(),但没有人让他的代码示例工作.让它工作的人最初使用某种形式的自定义消息泵来做这种事情,但我不知道如何实现这样的事情.

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.

推荐答案

我不认为你问的就是你真正想要的,但是为每个线程创建一个消息泵很容易,你只需要调用 Application.Run 一次每个线程.

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

这篇关于运行多个 UI 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆