为什么我可以从后台工作人员更改UI(.text属性) [英] Why am I able to change UI(.text property) from my background worker

查看:61
本文介绍了为什么我可以从后台工作人员更改UI(.text属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

曾经致力于在我的应用程序中集成后台工作人员,以帮助加快许多控件(50多个)的加载速度.最初,我将容纳所有这些控件逻辑的功能拆分为两个单独的功能-一个由 主UI线程,另一个将在backgroundworker上运行.它最终解决了很多问题,并且使窗口的加载时间和响应时间增加了几秒钟(巨大!),但是经过进一步的研究,我得知我将把用户界面的所有更改 将backgroundworker放到_progressChanged/runworkerCompleted事件中,因为它会将其扔回到主UI线程中,因此不会发生线程交叉.

Been working on integrating a backgroundworker on my application to help quicken the load of many controls(50+).  Originally, i split the function up that housed the logic for all these controls into two separate functions - one to be run by the main UI thread and the other to be run on the backgroundworker.  It ended up working out great and increasing load time and responsiveness of the window by several seconds(HUGE!!) but upon further research I learned I am to put any UI changes from the backgroundworker into the _progressChanged/runworkerCompleted event since it throws it back to the main UI thread so no crossing of threads will occur.

我的问题是,为什么我能够成功更改我的backgroundworkers的doWork事件上的许多控件的.text属性,并且没有问题?最初发生的是,我将看到主UI线程通过正常功能,并且 填充表单上的所有这些字段,然后我将看到背景工作人员的DoEvent代码运行并填充其余部分.只是试图了解为什么这里没有发生错误. :)

My question is, why was I able to succesfully change the .text properties of many controls on my backgroundworkers' doWork event and have no issues?  What would originally happen was I would see the main UI thread go through the normal function and populate all those fields on the form, then I would see the backgroundworkers' DoEvent code run through and populate the rest.  Just trying to get a sense of why no errors occured here. :)

谢谢!

推荐答案

跨线程调用不一定会产生错误.这些调用可以被捕获(如果以这种方式进行设置,则调试器将执行此操作),但无需这样做.但是建议这样做,因为这些调用可以创建 难以复制且难以调试的奇怪错误.因此,强烈建议不要跨线程调用.但这取决于您.参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx

A cross-thread call does not necessarily generate an error. These calls can be trapped (which is what the debugger does, if you set it up that way) but there is no requirement to do so.  It is, however, recommended, because these calls can create odd errors that are difficult to replicate and difficult to debug.  So cross-thread calls are strongly not recommended. But it's up to you.  See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx


这篇关于为什么我可以从后台工作人员更改UI(.text属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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