在WinForms中,UI中是否可以有多个线程? [英] In WinForms, Is More than One Thread Possible in the UI?

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

问题描述

我认为答案是否定的.我检查了关于stackoverflow的类似问题,但是根据特定解决方案的需要,它们似乎朝着不同的方向发展(但可能遗漏了一些东西).

I think the answer is no. I checked similar questions on stackoverflow, but they seem to go different directions based on what was needed for the specific solutions (but may have missed something).

在WinForms应用程序中是否可能有多个UI线程?我在重构并想知道是否应该使用从那些访问表单的线程访问的ConcurrentDictionary或Dictionary.这是针对具有多个开发人员/设计的更大的代码体,我希望做出尽可能可靠的选择(但不要过度设计).由于我正在寻找一个一般性的答案,因此尽管通常是具体的,但具体问题不在此问题中.感谢您的查看和任何帮助-非常感谢.

Is it ever possible to have more than a single UI thread in a WinForms application? I am refactoring and wondering if I should use ConcurrentDictionary or Dictionary that would be accessed from those thread(s) accessing forms. This is for a larger body of code with multiple developers/designs and I want to make the choice as solid as possible (but not over engineer it). Since I am looking for a general answer, specifics are not in this question although they usually are. Thanks for looking and any help - much appreciated.

推荐答案

是的,如果需要的话,完全有可能.这是一个使用相同形式的新实例生成线程的形式示例:

Yes, it’s perfectly possible if you need to. Here is an example of the form that spawns threads with new instances of the same form:

public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click( object sender, EventArgs e )
    {
        Thread t = new Thread( ThreadProc );
        t.Start();
    }

    static void ThreadProc()
    {
        Application.Run( new Form1() );
    }
}

但是,设计多线程GUI不一定是一个好主意.您必须了解后果.我建议您阅读有关该主题的MSDN文章.它是从1993年开始的,但是唯一不推荐使用的部分是有关从Windows 3.1迁移的信息.

But, designing multithreaded GUI is not necessarily a good idea. You have to understand the consequences. I suggest you read the MSDN article about the subject. It’s from 1993, however the only deprecated parts are about migration from windows 3.1.

这篇关于在WinForms中,UI中是否可以有多个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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