MDI Child在单独的线程中 [英] MDI Child in a separate thread

查看:76
本文介绍了MDI Child在单独的线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要有关MDI应用程序的一些信息.
我需要创建一个MDI应用程序,但是MDI应用程序的每个子窗体都会有很多UI操作.因此,我想在一个单独的线程中创建MDI子代,并让它自己运行,即,如果使MDI子代挂断/崩溃的任何原因,MDI父代仍然继续.
我发现了一些类似的示例,但是它们是在WPF中开发的.
对我来说,我需要使用Windows Forms MDI应用程序.
有可能这样做吗?
提供任何相关信息将不胜感激.

谢谢,致以最诚挚的问候,
Zaw Min Tun

Hi all,

I need some information regarding to MDI application.
I need to create an MDI apllication, but each child form of the MDI application will be having a lot of UI operations for itself. So I want to create the MDI child in a separate thread and let it run by itself, i.e. if anything make that MDI child hang up/crash, the MDI parent will still go on.
I found some similar samples, but they are developed in WPF.
For me, I need to use the Windows Forms MDI application.
Is it possible to do so?
Providing any relating information will be very grateful.

Thanks & best regards,
Zaw Min Tun

推荐答案

错误的想法.线程与窗体和控件无关.您可以运行不同的线程,每个线程都更新相同和不同的控件.让我们看看.

首先,如何运行线程并使用UI.

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

现在,我怀疑您是否真的需要MDI.你真的不想要它.首先,所有人和Microsoft强烈不建议使用此样式.使用MDI实施更困难,并且您不希望它给用户带来极大的不便.

请参阅:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [在WPF中使用MDI窗口的问题 [ ^ ],
提供错误的MDIContainer [
Wrong idea. Threads are not related to Forms and Controls. You can run different threads each updating the same and different controls. Let''s see.

First, how to run threads and work with UI.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Now, I doubt you really need MDI. You don''t want it, really. This style is highly discouraged by everyone, and Microsoft, first of all. Implementation using MDI is more difficult, and you don''t want to scare off your users with immense inconvenience of it.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^].

In my past solutions referenced above I also suggest what to do instead.

—SA


尊敬的SAKryukov,

非常感谢您提供答案和链接.
我将浏览链接并学习.

但是,我可以再提出一个幼稚的问题吗?说调用Invoke或BeginInvoke将强制代码行在创建该Control或MDI Child的同一线程中运行是正确的吗?

再次感谢您的宝贵时间.

最好的问候,
Zaw Min Tun
Dear SAKryukov,

Thanks a lot for the answer and the links.
I will browse the links and learn.

But, can I make one more naive question? Is it correct to say that Calling Invoke or BeginInvoke will force the code lines to run back in the same thread which created that Control or MDI Child?

Thanks again for your time, a lot.

Best regards,
Zaw Min Tun


要在最高级别回答您的问题,所有用户界面(UI)更新都将发送回主线程(UI线程).任何计算等都将在您的辅助线程上处理.因此,例如,如果您更改文本框的text属性,则在使用Invokek时将其分派到主线程,但是如果您首先计算该文本框的值,那么它将在您的辅助线程上进行计算.

请阅读上面的文章,该文章非常有帮助,并且是由博学的人撰写的.

亲切的问候

特伦斯(Terence)
To answer your question at top level, all the user interface (UI) updates will be sent back to the main thread, (UI Thread). Any calculations ect will be handled on your secondary thread. So for instance, if you change the text property of a textbox, it will be dispatched to the main thread if you use Invokek, but if you first calculate the value of a the textbox then it will be calculated on your secondary thread.

Please read the articles above, it is very informative and written by a knowledgeable person.

Kind Regards

Terence


这篇关于MDI Child在单独的线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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