避免跨线程调用 [英] Avoiding Cross thread calls

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

问题描述

大家好


我有一个带有gui的表单,只有一些列表框。


和一个处理传入数据的类。


i想要在接收数据时更新我的​​gui。


但是即使我在我的班级中创建了一个实例来处理

接收gui表单的数据如果我尝试直接更新gui我得到一个

交叉线程错误,试图从其他线程访问表单列表框

是创建它的那个。


因为调用gui update方法的方法是异步的。


如何我绕过这个,我已经尝试使用事件和代表获得

类,在接收数据时,触发事件然后将该事件挂钩到

gui so它会在事件发生时更新,但这仍然会导致同样的错误。


这一定是可能的吗?

解决方案

SORRY解决了它。


对于有类似问题的其他人,您需要使用Invoke从控制线程调用

更新。


所以在我的情况下我的班级接收传入的连接我做了一些事情

喜欢


_gui.Invoke(new UpdateGuiCallBack(_gui.Update));
http://blogs.msdn.com /csharpfaq/arch.../17/91685.aspx


Daniel <沓***** @ vestryonline.com>在消息中写道

新闻:uV ************** @ TK2MSFTNGP05.phx.gbl ...

嗨伙计们>
我有一个带有gui的表单,只有一些列表框。

和一个处理传入数据的类。

我想,在接收数据时,更新我的gui。

然而,即使我在我的班级创建了一个实例处理
接收gui表单的数据,如果我尝试直接更新gui我得到
一个交叉线程错误,试图从一个线程访问表单列表框
其他创建它的那个。

因为调用gui update方法的方法是异步的。 />
我如何绕过这个,我尝试使用事件和代表获取类,在接收数据时,触发事件然后将该事件挂钩到gui所以它关于事件发生的更新,但这仍然会导致相同的错误。

这一定是可能的吗?



>我有一个带有gui的表单,只有一些列表框。


还有一个处理传入数据的类。

我希望在接收数据时,更新我的gui。

然而,即使我有一个在我的类中创建的实例处理gui表单的接收数据,如果我尝试直接更新gui我得到一个跨线程错误,尝试从创建它的那个线程中访问表单列表框。

因为调用gui update方法的方法是异步的。
我如何绕过这个,我已经尝试使用事件和代表获取类,在接收数据时,触发事件
然后将该事件挂钩到gui,以便它在事件触发时更新但这仍然导致同样的错误。

这一定是可能的吗?




尝试将表单的CheckForIllegalCrossThreadCalls属性设置为false。或者看看这里:
http:// msdn2.microsoft.com/en-us/library/ms171728.aspx

希望这会有所帮助,


Mike

- Microsoft Visual Basic MVP -

电子邮件: ED***@mvps.org

WWW: Http://ryais.mvps.org/


嗨Mike


已经解决了,但谢谢。虽然你的回复很有意思,但如果我要用b $ b,请使用:


CheckForIllegalCrossThreadCalls


property,wouldn'压抑支票有危险吗?难道我不能在我的软件中因为交叉线程而导致
错误吗?


" Mike D Sutton" < ED *** @ mvps.org>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...

我有一个带有gui的表单,只有一些列表框。

还有一个处理传入数据的类。

我希望收到数据,更新我的gui。

然而即使我在我的班级中创建了一个实例来处理
接收gui表单的数据如果我尝试直接更新gui我得到
一个交叉线程错误,试图从一个线程访问表单列表框
其他创建它的那个。

因为调用gui update方法的方法是异步的。 br />
我如何解决这个问题,我尝试过使用事件和代表来获取类,接收数据,触发事件然后将该事件挂钩到gui所以它会在事件发生时更新,但这仍然会导致相同的错误。

这一定是可能的吗?



试试将表单的CheckForIllegalCrossThreadCalls属性设置为false。或者看看这里:
http:// msdn2.microsoft.com/en-us/library/ms171728.aspx
希望这会有所帮助,

迈克

- Microsoft Visual Basic MVP -
电子邮件: ED***@mvps.org
WWW: Http://EDais.mvps.org/



Hi guys

I have a form with my gui on, just some list boxes.

And a class that handles incoming data.

i want to, on receiving data, update my gui.

However even though i have an instance created in my class handling the
receiving data of the gui form if i try and update the gui directly i get a
cross thread error, trying to access the form listbox from a thread other
that the one it was created on.

its because the method that called the gui update method is asynchronous.

How do i get round this, i have tried using events and delegates getting the
class to, on receving data, fire an event and then hook that event to the
gui so it updates on the event firing but this still causes the same error.

This must be possible right?

解决方案

SORRY solved it.

For anyone else with a similar problem you need to use Invoke to call the
update fromt he controls thread.

So in my case in my Class taking the incoming connections i do something
like

_gui.Invoke(new UpdateGuiCallBack(_gui.Update));

http://blogs.msdn.com/csharpfaq/arch.../17/91685.aspx

"Daniel" <Da*****@vestryonline.com> wrote in message
news:uV**************@TK2MSFTNGP05.phx.gbl...

Hi guys

I have a form with my gui on, just some list boxes.

And a class that handles incoming data.

i want to, on receiving data, update my gui.

However even though i have an instance created in my class handling the
receiving data of the gui form if i try and update the gui directly i get
a cross thread error, trying to access the form listbox from a thread
other that the one it was created on.

its because the method that called the gui update method is asynchronous.

How do i get round this, i have tried using events and delegates getting
the class to, on receving data, fire an event and then hook that event to
the gui so it updates on the event firing but this still causes the same
error.

This must be possible right?



> I have a form with my gui on, just some list boxes.


And a class that handles incoming data.

i want to, on receiving data, update my gui.

However even though i have an instance created in my class handling the receiving data of the gui form if i try and
update the gui directly i get a cross thread error, trying to access the form listbox from a thread other that the one
it was created on.

its because the method that called the gui update method is asynchronous.

How do i get round this, i have tried using events and delegates getting the class to, on receving data, fire an event
and then hook that event to the gui so it updates on the event firing but this still causes the same error.

This must be possible right?



Try setting the CheckForIllegalCrossThreadCalls property of your form to false. Alternatively have a look here:
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
Hope this helps,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/


Hi Mike

Solved it already but thanks. Though your reply was interesting, if i was to
use the:

CheckForIllegalCrossThreadCalls

property, wouldn''t that be dangerous to surpress the check? Couldn''t i have
bugs in my software caused by the cross thread?

"Mike D Sutton" <ED***@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

I have a form with my gui on, just some list boxes.

And a class that handles incoming data.

i want to, on receiving data, update my gui.

However even though i have an instance created in my class handling the
receiving data of the gui form if i try and update the gui directly i get
a cross thread error, trying to access the form listbox from a thread
other that the one it was created on.

its because the method that called the gui update method is asynchronous.

How do i get round this, i have tried using events and delegates getting
the class to, on receving data, fire an event and then hook that event to
the gui so it updates on the event firing but this still causes the same
error.

This must be possible right?



Try setting the CheckForIllegalCrossThreadCalls property of your form to
false. Alternatively have a look here:
http://msdn2.microsoft.com/en-us/library/ms171728.aspx
Hope this helps,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/



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

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