这样安全吗? [英] Is this safe

查看:94
本文介绍了这样安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个工作线程可能需要为用户显示一个对话框。对于工作线程调用表单的构造函数然后传递

表单'对主GUI线程的引用ShowDialog(是)安全吗? )"然后将调用
。我假设它可能是因为表单永远不会显示在

实际工作线程上。有人可以确认这是安全的。谢谢。

Hi there,

I have a worker thread that may need to display a dialog for the user. Is it
safe for the worker thread to invoke the form''s constructor and then pass
the form''s reference to the main GUI thread where "ShowDialog()" will then
be invoked. I assume it probably is since the form is never displayed on the
actual worker thread. Can someone confirm this is safe. Thanks.

推荐答案

" Michael Chambers" < no_spam@_nospam.com写信息

新闻:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
"Michael Chambers" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

你好,


我有一个工作线程,可能需要为用户显示一个对话框。是

工作线程调用表单的构造函数然后

将表单的引用传递给主GUI线程,其中ShowDialog是安全的)"将

然后被调用。我假设它可能是因为表单永远不会在实际工作线程上显示

。有人可以确认这是安全的。谢谢。
Hi there,

I have a worker thread that may need to display a dialog for the user. Is
it safe for the worker thread to invoke the form''s constructor and then
pass the form''s reference to the main GUI thread where "ShowDialog()" will
then be invoked. I assume it probably is since the form is never displayed
on the actual worker thread. Can someone confirm this is safe. Thanks.



我认为你不能认为它是安全的。是否安全取决于

是否已创建对话框的窗口句柄,以及确切创建

窗口句柄的时间取决于对话框的具体方式访问,

包括可能来自在表单构造函数中运行的代码。


我准备错了:)我怀疑它实际上很安全,很多情况下很好,但是我不愿意这么做。


-cd

I don''t think you can assume that it''s safe. Whether it''s safe depends on
whether the dialog''s window handle has been created, and exactly when the
window handle is created depend on exactly how the dialog is accessed,
including potentially from code that runs in the constructor of the form.

I''m prepared to be wrong about that :) I suspect that it is in fact safe in
many cases, but I wouldn''t bank on it.

-cd




Carl Daniel [VC ++ MVP]写道:

Carl Daniel [VC++ MVP] wrote:

我准备错了: )我怀疑它实际上很安全

许多情况下,但我不会依赖它。
I''m prepared to be wrong about that :) I suspect that it is in fact safe in
many cases, but I wouldn''t bank on it.



Carl,


我同意。我也不会依赖它。另外,如果你已经在UI线程上调用ShowDialog,那么为什么

也没有在那里实例化Form。


Brian

Carl,

I agree. I wouldn''t bank on it either. Plus, if you''re already
putting forth the effort to call ShowDialog on the UI thread then why
not instantiate the Form there too.

Brian


>我有一个工作线程,可能需要为用户显示一个对话框。是
>I have a worker thread that may need to display a dialog for the user. Is

>工作者线程可以安全地调用表单的构造函数然后
传递表单''参考主GUI线程,然后调用ShowDialog()
。我认为可能是因为表单永远不会显示在实际的工作线程上。有人可以证实这是安全的。
谢谢。
>it safe for the worker thread to invoke the form''s constructor and then
pass the form''s reference to the main GUI thread where "ShowDialog()"
will then be invoked. I assume it probably is since the form is never
displayed on the actual worker thread. Can someone confirm this is safe.
Thanks.



我认为你不能认为它是安全的。是否安全取决于

是否已创建对话框的窗口句柄,以及确切创建

窗口句柄的时间取决于对话框的具体方式访问,

包括可能来自在表单构造函数中运行的代码。


我准备错了:)我怀疑它实际上是安全的

在许多情况下,但我不会依赖它。


I don''t think you can assume that it''s safe. Whether it''s safe depends on
whether the dialog''s window handle has been created, and exactly when the
window handle is created depend on exactly how the dialog is accessed,
including potentially from code that runs in the constructor of the form.

I''m prepared to be wrong about that :) I suspect that it is in fact safe
in many cases, but I wouldn''t bank on it.



感谢您的反馈。我是那个正在创作这个表单的人,然而

构造函数总是只执行简单的初始化(没有窗口

句柄都是创建的)。由于表单最初是由VS

表单'的设计者创建的,但是我不能确定是什么" InitializeComponent()"将

做。我严重怀疑它是否曾经处理过Windows手柄(毕竟这是一个建设者)但你永远都不知道。希望有人能够澄清这种情况。无论如何,再次感谢。

Thanks for the feedback. I''m the one who''s authoring this form however and
the constructor will always perform simple initialization only (no window
handles are ever created). Since the form is originally created by the VS
form''s designer however, I can''t be sure what "InitializeComponent()" will
do. I seriously doubt that it ever deals with windows handles however (this
is a constructer after all) but you never know. Hopefully someone can
clarify the situation. Anyway, thanks again.


这篇关于这样安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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