如何从线程调用表单 [英] how to calling a form from a thread

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

问题描述

大家好,

我无法在线程中调用表单的Show()/ShowDialog()
我搜索了google,但找不到对我有用的东西?

任何人都可以给我一些指导代码以实现此调用吗?

在此先感谢
----------
一些解释:

好吧,我试图编写一个处理器管理器"来测试我的技能,在这种情况下,有时在某些情况下程序必须弹出某种形式才能通知用户程序背景中发生的事情,或请求许可以对处理器中的某些有害/暴力进程进行操作.....
正如我到目前为止所编程的那样,这些性能中的每一个几乎都在4个线程中发生.
进程验证检查通过程序在线程中的运行时间进行....
为了处理这种形式,我创建了一个"FormMonitoring"类,该类将一个表单对象传递给该类,并且该类将该表单排队,然后显示该表单,这是我的第一个设计,旨在处理我之前解释过的内容!
嗯,我有2个选择:
1)更改我的格式化策略:我对此没有任何其他想法!!!!!!:confused:[任何想法?]
2)在线程中调用表格:((< 我也尝试调用表单,但是结果是一样的,窗口出现了,但是控件从未创建!
关于这个问题的任何想法,这是我第一次遇到这种问题? 下图是我尝试显示表单时的结果![当我尝试调用表单时,结果几乎相同!]

屏幕截图 [

Hey guys,

I can''t call a form''s Show()/ShowDialog() within a thread!!!

I searched google, but i couldn''t find something useful for me?

Can any one give me some guide code in order to implement this calling??

Thanks in advance
----------
some explanation:

well i''m trying to write a '' Processor Manager '' to test my skills and in here there are sometimes which in some circumstance program have to pop up some form in order to notify user the things happened in program''s background, or ask permission to do something with some unwanted/violent process(es) in processor .....
every each of these performance happens in threads almost in 4 threads, as i programmed till now ...
the processes validation checking runs through program''s run time in threads ....
in orther to handle this formming i create a '' FormMonitoring '' Class, which pass a form object into this, and this class queue this form and then show this form, this was my first design in order to handle what i explained before!
well there are 2 options for me :
1) change my formming strategy : i have not any other idea about this!!!??:confused:[Any idea?]
2) call forms in the thread :((
i also tried invoke the form but the result is same, the windows cames up, but the controls never create!!
any idea about this problems, this is first time i encounter with this kind of problem??:~ :doh:
the following picture is the result when i try to show a form![the result is ALMOST same when i try to invoke the form!]

A screen shot[^]

推荐答案

您永远不要这样做! ShowDialog是模态的.只是想想您要做什么.

您应该重新考虑设计;如果您能说明目标,就可以获得帮助.

更一般而言,即使没有模式行为,也无法直接从非UI线程操作UI.您应该改用InvokeBeginInvoke.这将解释所有详细信息:
Control.Invoke()与Control.BeginInvoke( ) [ ^ ], Treeview扫描仪和MD5的问题 [ ^ ].

—SA
You should never do that! ShowDialog is modal. Just think a bit of what are you trying to do.

You should re-think you design; if your explain your goal, you can get some help.

More generally, you cannot directly operate UI from non-UI thread even without modal behavior. You should use Invoke or BeginInvoke instead. This will explain all the detail: Control.Invoke() vs. Control.BeginInvoke()[^], Problem with Treeview Scanner And MD5[^].

—SA


您必须使用Invoke.

Google是您的朋友: [
You have to use Invoke.

Google is your friend: Search results for "C# ui update from thread"[^]


这篇关于如何从线程调用表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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