是否有必要从工作线程使用Invoke for User Controls? [英] Is it necessary to use Invoke for User Controls from a Worker Thread?

查看:72
本文介绍了是否有必要从工作线程使用Invoke for User Controls?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主线程上创建了一个用户控件。让我们说,为了论证

,它有一个维护私有变量的属性。如果我想要从工作线程设置该属性,我是否需要使用

UserControl1.Invoke来设置它,或者我可以设置它吗?毕竟,改变私人变量只是




TIA


Charles

I have a user control created on the main thread. Let''s say, for arguments
sake, that it has a single property that maintains a private variable. If I
want to set that property from a worker thread, do I need to use
UserControl1.Invoke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles

推荐答案

查尔斯


如果变量是控件 - 答案是你必须使用Invoke。如果是与UI无关的某个对象

,则可以使用lock语句进行简单设置。锁定 -

因为即使它是私有变量,如果你有超过1个线程,你可能会有同步问题。


您可能需要检查.Net

文档中的lock语句和volatile修饰符以获取更多详细信息。


HTH

Alex


" Charles Law" < BL *** @ nowhere.com>在消息中写道

新闻:OJ ************** @ TK2MSFTNGP11.phx.gbl ...
Hi, Charles

if variable is control - answer is you must use Invoke. If it is some object
non-related to UI, you can simply set it using lock statement. Lock -
because even if it is private variable if you have more than 1 thread you
might have synchronization problems.

You might want to check lock statement and volatile modifier in .Net
documentation to get more details.

HTH
Alex

"Charles Law" <bl***@nowhere.com> wrote in message
news:OJ**************@TK2MSFTNGP11.phx.gbl...
我有一个用户控件在主线程上创建。让我们说,为了论证,它有一个维护私有变量的属性。如果
我想从工作线程设置该属性,我是否需要使用
UserControl1.Invoke来设置它,还是我可以设置它?毕竟,它只是改变一个私人变量。

TIA

Charles
I have a user control created on the main thread. Let''s say, for arguments
sake, that it has a single property that maintains a private variable. If I want to set that property from a worker thread, do I need to use
UserControl1.Invoke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles






----- Charles Law写道:----


毕竟,它只是在改变一个私人变量


着名的最后一句话


----- Charles Law wrote: ----

After all, it is only changing a private variable

Famous last words


如果你忽略线程安全问题,答案是否定的,你不要''需要使用

调用从另一个线程更新私有字段。如果您确实更新了屏幕,或者执行直接或间接操作的操作,那么
调用一个对线程敏感的API,那么您需要使用Invoke。


" Charles Law" < BL *** @ nowhere.com>在消息中写道

新闻:OJ ************** @ TK2MSFTNGP11.phx.gbl ...
If you ignore thread-safety issues, the answer is no, you don''t need to use
Invoke to update private fields from another thread. If you are actually
updating the screen, or perform an operation which directly or indirectly
calls an API that is thread sensitive, then you need to use Invoke.

"Charles Law" <bl***@nowhere.com> wrote in message
news:OJ**************@TK2MSFTNGP11.phx.gbl...
我有一个用户控件在主线程上创建。让我们说,为了论证,它有一个维护私有变量的属性。如果
我想从工作线程设置该属性,我是否需要使用
UserControl1.Invoke来设置它,还是我可以设置它?毕竟,它只是改变一个私人变量。

TIA

Charles
I have a user control created on the main thread. Let''s say, for arguments
sake, that it has a single property that maintains a private variable. If I want to set that property from a worker thread, do I need to use
UserControl1.Invoke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles



这篇关于是否有必要从工作线程使用Invoke for User Controls?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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