'跨线程操作无效'异常/多线程组件 [英] 'cross-thread operation not valid' exception / multithreaded compo

查看:53
本文介绍了'跨线程操作无效'异常/多线程组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


在MSDN讨论组的

中有几个''跨线程操作无效'的例外帖子,但没有一个解决我的问题确切的情况。


我在Visual C#.NET 2003中编写了一个.NET组件,能够发出几种类型的事件来通知用户错误,数据更改等。

组件使用线程执行后台通信,如果有
是错误或数据更改触发事件以通知用户。如果使用我的组件的

开发人员希望显示值表格

控件的Text属性,则''跨线程操作无效''例外是

在VS.NET 2005中抛出。


我明白为什么抛出它(工作线程不允许更新UI

线程),但是找不到任何关于这是否可以从组件角度解决的确切信息,而不是要求我的

组件的用户使用代表和IsInvokeRequired / Invoke显示

值。


任何反馈或建议都将不胜感激。

Hello,

There are several ''cross-thread operation not valid'' exception postings in
the MSDN discussion groups, but none address my exact situation.

I have authored a .NET component in Visual C# .NET 2003 that is capable of
firing several types of events to notify user of errors, data changes, etc.
The component uses a thread to perform background communications and if there
is an error or data-change fires an event to notify the user. If the
developer that is using my component wishes to display the value form
control''s Text property, the ''cross-thread operation not valid'' exception is
thrown in VS.NET 2005.

I understand why it is thrown (worker thread not allowed to update UI
thread), but can''t find any definitive information on whether this is
addressable from the component standpoint, rather than requiring my
component''s user to use delegates and IsInvokeRequired/Invoke to display the
values.

Any feedback or suggestions would be appreciated.

推荐答案

不,没有必要要求组件的用户使用

代表或任何这些东西。完全可以隐藏用户的

线程,以及托管组件的形式。

请记住,组件本身与表单。

它是由另一个线程中的组件生成的线程。因为你的组件本身在内部管理编组并且以线程的形式触发
,所以它应该不是问题。
< br $> b $ b -

HTH,

Kevin Spencer

Microsoft MVP

专业Numbskull


如果没有作品,请告诉我你的认证,

我将展示我的认证

* by * my工作。


" HairlipDog58" <哈******** @ nospam.nospam>在消息中写道

news:95 ********************************** @ microsof t.com ...
No, it''s not necessary to require the user of the component to use
delegates, or any of that stuff. It is entirely possible to hide the
threading from the user, and the form in which the component is hosted.
Remember that the component itself resides in the same thread as the form.
It is the thread spawned by the component which is in another thread. As
long as your component itself manages the marshalling internally and fires
the event in the form''s thread, it should not be a problem.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I''ll show my certification
*by* my works.

"HairlipDog58" <Ha********@nospam.nospam> wrote in message
news:95**********************************@microsof t.com...
你好,

在MSDN讨论组中有几个''跨线程操作无效''例外帖子,但是没有解决我的确切情况。

我在Visual C#.NET 2003中编写了一个.NET组件,它能够触发几种类型的事件来通知用户错误,数据更改,<
组件使用线程来执行后台通信,如果
出现错误或数据更改则会触发事件以通知用户。如果正在使用我的组件的开发人员希望显示值表单
控件的Text属性,那么''跨线程操作无效''例外

在VS.NET 2005中抛出。

我理解为什么抛出它(工作线程不允许更新UI
线程),但是找不到关于这是否是<的确切信息从组件的角度来看,可以寻址,而不是要求我的
组件的用户使用委托和IsInvokeRequired / Invoke来显示
值。

任何反馈或建议将不胜感激。
Hello,

There are several ''cross-thread operation not valid'' exception postings in
the MSDN discussion groups, but none address my exact situation.

I have authored a .NET component in Visual C# .NET 2003 that is capable of
firing several types of events to notify user of errors, data changes,
etc.
The component uses a thread to perform background communications and if
there
is an error or data-change fires an event to notify the user. If the
developer that is using my component wishes to display the value form
control''s Text property, the ''cross-thread operation not valid'' exception
is
thrown in VS.NET 2005.

I understand why it is thrown (worker thread not allowed to update UI
thread), but can''t find any definitive information on whether this is
addressable from the component standpoint, rather than requiring my
component''s user to use delegates and IsInvokeRequired/Invoke to display
the
values.

Any feedback or suggestions would be appreciated.



尊敬的客户,


除了凯文的建议,我想你可以试着看看

以下三篇关于如何从另一个线程更新UI的文章

你的参考。

Windows窗体中安全,简单的多线程,第1部分
http://msdn.microsoft.com/library/de...us/dnforms / htm

l / winforms06112002.asp

Windows窗体中安全,简单的多线程,第2部分
http://msdn.microsoft.com/library/de .. .us / dnforms / htm

l / winforms08162002.asp

Windows窗体中安全,简单的多线程,第3部分
http://msdn.microsoft.com/library /de...us/dnforms/htm

l / winforms01232003.asp

因为几乎每个winform相关类都不是线程安全的,我们必须

为winform线程上的所有调用编组。

.Net提供了一个简单的方法,Control.Invoke会整理电话

到线程所在的位置控制是。此外,我们可以使用SendMessage来支持
,这是Control.Invoke所做的。


祝你好运,


Peter Huang


Microsoft在线社区支持

==================== ==============================

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子按原样提供。没有保证,也没有授予任何权利。

Dear Customer,

In addition to Kevin''s suggestion, I think you may try to take a look at
the three articles below about how to update UI from another thread for
your reference.

Safe, Simple Multithreading in Windows Forms, Part 1
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms06112002.asp
Safe, Simple Multithreading in Windows Forms, Part 2
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms08162002.asp
Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/de...us/dnforms/htm
l/winforms01232003.asp

As almost every winform related class is not thread-safe, we have to
marshal all the call on the winform thread.
The .Net provides a simple method, Control.Invoke will marshal the call
onto the thread where the Control is. Also we can use SendMessage to
marshal the call, which is what the Control.Invoke do.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


嗨彼得,


作为一个组成部分,我不喜欢无法访问目标UI控件,并且不知道

将从事件接收器更新哪种类型的控件。


您建议的文章所有这些都描述了一个单片应用程序,其中

a线程在Win Forms应用程序中启动,并且事件接收器具有

知道将要更新的Win表单控件的类型。 br />

我开始沿着Kevin描述的路径开始,但是我遇到了麻烦

搞清楚如何从工作线程通知我的组件事件<需要解雇
。我已经尝试设置一个委托并使用BeginInvoke,

但是抛出了同样的异常。
Hi Peter,

As a component, I don''t have access to the target UI control and do not know
what type of control will be updated from the event sink.

The articles that you suggested all describe a monolithic application where
a thread is started in a Win Forms application, and the event sink has
knowledge of the type of Win form control that will be updated.

I started down the path that Kevin described, but I''m having trouble
figuring out how to notify my component from the worker thread that an event
needs to be fired. I''ve tried setting up a delegate and using BeginInvoke,
but the same exception is thrown.
我们也可以使用SendMessage来编组调用,这是Control.Invoke的功能。
Also we can use SendMessage to marshal the call, which is what the Control.Invoke do.




你能详细说明.NET中的SendMessage用法吗?



Can you elaborate on SendMessage usage in .NET?

这篇关于'跨线程操作无效'异常/多线程组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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