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

查看:89
本文介绍了运行多个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天全站免登陆