(VS 2005 Beta)非法的跨线程操作 [英] (VS 2005 Beta)Illegal cross-thread operation

查看:57
本文介绍了(VS 2005 Beta)非法的跨线程操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该做些什么来回退我在VS2003中的许可,允许

我从其他线程访问一个类的公共方法。


我有A级(FORM)创建新线程(新B级),新类

线程作为参数引用给他的父亲(CLASS谁疯了(classA)),

新线程需要调用他父亲的方法,如myfathe.methode(),

这在VS 2003中有效但现在在VS 2005 Beta中抛出异常:


发生了'System.InvalidOperationException'类型的未处理异常
System.Windows.Forms.dll中的


附加信息:非法的跨线程操作:控制

''Green_pictureBox1''从一个线程以外的线程访问

创建。

发生非法操作的堆栈跟踪是:在System.Windows.Forms上的System.Windows.Forms.Control.get_Handle()


。 Control.SetVisibleCore(Boolea n)

at System.Windows.Forms.Control.set_Visible(Boolean)

at Mynamespace.Test_Board_Main_Form.Green_originalima geI_Light(Boolean)

。在Mynamespace.ExecuteThread.FirmwareUpdateDll()

解决方案
[Yosi]<哟** @ discussions.microsoft.com>写道:我应该怎么做,返回回permissin我在VS2003中,allowd
我从其他线程访问一个类的公共梅索德


这不是一个类,它是一个UI类。虽然改变来自另一个线程的UI元素的属性,但有时*可以*与.NET 1.1一起使用,但它仍然是一个非常糟糕的主意*。


http:// www .pobox.com /~subet / csharp / t ... winforms.shtml


我很高兴看到它现在被提前发现而不是允许破碎的

代码有时工作。


-

Jon Skeet - < sk *** @ pobox.com> ;
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


在VS2003中你做的事情非常糟糕(tm)。您我们正在从底层窗口系统不支持的错误线程更新UI。 VS2005现在可以防止你在脚下射击。


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


我应该做些什么来返回我在VS2003中的权限,允许

我从其他线程访问一个类的公共方法。


我有A级(FORM)创建新线程(新B级),新类

线程作为参数引用给他的父亲(CLASS谁疯了(classA)),

新线程需要调用他父亲的方法,如myfathe.methode(),

这在VS 2003中有效但现在在VS 2005 Beta中抛出异常:


类型''System.InvalidOperationException''occ的未处理异常在System.Windows.Forms.dll中支持




附加信息:非法的跨线程操作:控制

''Green_pictureBox1 ''从线程以外的线程访问

创建。

发生非法操作的堆栈跟踪:


。在System.Windows.Forms.Control.get_Handle()

。在System.Windows.Forms.Control.SetVisibleCore(Boolea n)的在System.Windows.Forms的

.Control.set_Visible(Boolean)

at Mynamespace.Test_Board_Main_Form.Green_originalima geI_Light(Boolean)

at Mynamespace.ExecuteThread.FirmwareUpdateDll()


Jon,


请注意,只有在调试器中运行时才会被捕获,没有任何更改

in非调试模式。


Willy。


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

[Yosi]< Yo ** @ discuss.microsoft.com>写道:我应该怎么做,返回回permissin我在VS2003,那

从其他线程访问一个类的公共梅索德

这不是一个类,它是一个UI类。虽然从另一个线程更改UI元素的属性会有时*与.NET 1.1一起使用,但它仍然是一个非常糟糕的主意*。

请参阅 http://www.pobox.com/~skeet/csharp/t。 ..winforms.shtml

我很高兴看到它现在被提前发现而不是允许破碎的代码有时工作。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



What I should do to return back the permissin I had in VS2003 , that allowd
me to access public methode of one Class from other Thread.

I have Class A(FORM) which create new thread (new class B), the new class
thread get as parameter reference to his father( CLASS who mad it (classA)),
The new thread need to call methodes from his father like myfathe.methode(),
This worked in VS 2003 but now in VS 2005 Beta throw an exception :

An unhandled exception of type ''System.InvalidOperationException'' occurred
in System.Windows.Forms.dll

Additional information: Illegal cross-thread operation: Control
''Green_pictureBox1'' accessed from a thread other than the thread it was
created on.
Stack trace where the illegal operation occurred was:

at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetVisibleCore(Boolea n)
at System.Windows.Forms.Control.set_Visible(Boolean)
at Mynamespace.Test_Board_Main_Form.Green_originalima geI_Light(Boolean)
at Mynamespace.ExecuteThread.FirmwareUpdateDll()

解决方案

[Yosi] <Yo**@discussions.microsoft.com> wrote:

What I should do to return back the permissin I had in VS2003 , that allowd
me to access public methode of one Class from other Thread.



It''s not that it''s a class, it''s that it''s a UI class. Although
changing properties on UI elements from another thread would
*sometimes* work with .NET 1.1, it was still a *very bad idea*.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

I''m glad to see it''s now being caught early rather than allowing broken
code to work sometimes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


In VS2003 you were doing something Very Bad (tm). You we''re updating the UI from the wrong thread which is not supported by the underlying windowing system. VS2005 now catches this to prevent you from shooting yourself in the foot.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

What I should do to return back the permissin I had in VS2003 , that allowd
me to access public methode of one Class from other Thread.

I have Class A(FORM) which create new thread (new class B), the new class
thread get as parameter reference to his father( CLASS who mad it (classA)),
The new thread need to call methodes from his father like myfathe.methode(),
This worked in VS 2003 but now in VS 2005 Beta throw an exception :

An unhandled exception of type ''System.InvalidOperationException'' occurred
in System.Windows.Forms.dll

Additional information: Illegal cross-thread operation: Control
''Green_pictureBox1'' accessed from a thread other than the thread it was
created on.
Stack trace where the illegal operation occurred was:

at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetVisibleCore(Boolea n)
at System.Windows.Forms.Control.set_Visible(Boolean)
at Mynamespace.Test_Board_Main_Form.Green_originalima geI_Light(Boolean)
at Mynamespace.ExecuteThread.FirmwareUpdateDll()


Jon,

Note that it''s only caught when running in the debugger, nothing has changed
in non debug mode.

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

[Yosi] <Yo**@discussions.microsoft.com> wrote:

What I should do to return back the permissin I had in VS2003 , that
allowd
me to access public methode of one Class from other Thread.



It''s not that it''s a class, it''s that it''s a UI class. Although
changing properties on UI elements from another thread would
*sometimes* work with .NET 1.1, it was still a *very bad idea*.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

I''m glad to see it''s now being caught early rather than allowing broken
code to work sometimes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于(VS 2005 Beta)非法的跨线程操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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