背景工作者和控制 [英] Backgroundworker and controls

查看:81
本文介绍了背景工作者和控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要花费一些时间的因素,从而减慢了主要的
系统。我想把它放在一个backgroundworker组件中来运行它

asynchroneously。但是在程序中,我想在我的主窗体上更新一个TreeView控件。


当我尝试这样做时,我得到了错误跨线程操作无效:

控制''TreeView1''从线程以外的线程访问

创建于


我之前似乎已经读过一些关于这个问题的内容了,但是如果有解决方案的话,我不记得

......


请尽快建议,

Johnny J.

解决方案

" Johnny J?rgensen" < jo ** @ altcom.seschrieb


您好,我需要一些时间,从而减慢了

主系统的速度。我想把它放在一个backgroundworker组件中来运行

它是异步的。但是在程序中,我想更新我在主窗体上的一个

TreeView控件。


当我尝试这样做时,我收到错误跨线程操作不是

有效:控制''TreeView1''从除了

线程之外的线程访问


我之前似乎已经读过关于这个问题的一些事情了,但是不能确认是否有一个解决方案...


请尽快建议,

Johnny J.



调用控件的BeginInvoke函数([F1]了解详情) 。调用的

函数将在创建控件的线程中运行,允许

访问它。

Armin


但是不会和我主线程一样的线程???


/ Johnny J.

" Armin Zingler" < az ******* @ freenet.deskrev i meddelandet

news:uc ************** @ TK2MSFTNGP06.phx.gbl ...


" Johnny J?rgensen" < jo ** @ altcom.seschrieb


>嗨我有一个需要花费一些时间的因素,从而减慢了主系统的速度。我想把它放在一个backgroundworker组件中来运行
它异步。但是在程序中,我想更新我在主窗体上的TreeView控件。

当我尝试这样做时,我得到错误跨线程操作不是
有效:控制''TreeView1''从一个线程以外的线程访问

我似乎已经读过关于那个问题的东西了,但是可以'要记住是否有解决方案......

请尽快建议,
Johnny J.




调用控件的BeginInvoke函数([F1]了解详细信息)。调用的

函数将在创建控件的线程中运行,允许

来访问它。


Armin



但不会和我主线程的线程相同???


是的。


问题是后台工作者的主题不是你主要的
线程,这就是你需要使用BeginInvoke委托的原因控制

回到主线程。


谢谢,


Seth Rowe

Hi I have a procudure that takes some time and thus slows down the main
system. I want to put it in a backgroundworker component to run it
asynchroneously. But in the procedure, I want to update a TreeView control I
have on my main form.

When I try to do that, I get the error "Cross-thread operation not valid:
Control ''TreeView1'' accessed from a thread other than the thread it was
created on"

I seem to have read something about that poblem before, but can''t remember
if there''s a solution to it...

Please advice asap,
Johnny J.

解决方案

"Johnny J?rgensen" <jo**@altcom.seschrieb

Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworker component to run
it asynchroneously. But in the procedure, I want to update a
TreeView control I have on my main form.

When I try to do that, I get the error "Cross-thread operation not
valid: Control ''TreeView1'' accessed from a thread other than the
thread it was created on"

I seem to have read something about that poblem before, but can''t
remember if there''s a solution to it...

Please advice asap,
Johnny J.


Call the control''s BeginInvoke function ([F1] for details). The invoked
function will run in the thread that created the control, which is allowed
to access it.
Armin


But won''t that be the same thread as my main thread???

/Johnny J.

"Armin Zingler" <az*******@freenet.deskrev i meddelandet
news:uc**************@TK2MSFTNGP06.phx.gbl...

"Johnny J?rgensen" <jo**@altcom.seschrieb

>Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworker component to run
it asynchroneously. But in the procedure, I want to update a
TreeView control I have on my main form.

When I try to do that, I get the error "Cross-thread operation not
valid: Control ''TreeView1'' accessed from a thread other than the
thread it was created on"

I seem to have read something about that poblem before, but can''t
remember if there''s a solution to it...

Please advice asap,
Johnny J.



Call the control''s BeginInvoke function ([F1] for details). The invoked
function will run in the thread that created the control, which is allowed
to access it.
Armin



But won''t that be the same thread as my main thread???

Yes.

The problem is that the background worker''s thread is not your main
thread, which is why you need to use BeginInvoke to delegate control
back to the main thread.

Thanks,

Seth Rowe


这篇关于背景工作者和控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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