473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

(VS 2005 Beta)Illegal cross-thread operation

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()
Nov 16 '05 #1
12 2343
[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
Nov 16 '05 #2
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()

Nov 16 '05 #3
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

Nov 16 '05 #4
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
Note that it's only caught when running in the debugger, nothing has changed
in non debug mode.


Interesting. I suppose that's reasonable on performance grounds. Let's
just hope people do actually run things at least once in debug mode :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
[Yosi] <Yo**@discussions.microsoft.com> wrote:
Since Microsoft Allowd such a bad thing, there are product doing this bad
think , It's is so bad that Microsoft don't alowd this any more , if this
true, Microsoft must inform all those companies about this , so they can
decide if to buy the new VS2005 or not, (if to continue use the bad usage or
to make new release with the fix..)
I think they should add an option to allowd this or not (my in security
option list).


No. It's not something that they've just disallowed - it's *always*
been dangerous, and it's always been talked about all over the place.
It's not like it's not documented. It's just that now in debug mode the
mistakes can be found more clearly.

Your code was broken under VS.NET 2003 - it just wasn't as *obviously*
broken.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Yes, I guess they didn't want to burn some CPU cycles for this in a release
built, the problem is that you shouldn't need to run in the debugger for
this to show up. I'm pretty sure most of us will only unit-test when moving
existing applications to v2.0 and 'problems' like this will simply pass the
test runs.
I just finished a code review for a large number of windows forms
applications, you would be surprised of the number of applications failing
to obey this UI threading rule. The reactions (some) of the (professional)
developers goes like this: "Oh, it's probably the debugger" or "normal, this
thing is still in beta, right" or "MSFT broke my code" or "Never heard about
this and it's not documented" etc.

Willy.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
Note that it's only caught when running in the debugger, nothing has
changed
in non debug mode.


Interesting. I suppose that's reasonable on performance grounds. Let's
just hope people do actually run things at least once in debug mode :)

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

Nov 16 '05 #7
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
Yes, I guess they didn't want to burn some CPU cycles for this in a release
built, the problem is that you shouldn't need to run in the debugger for
this to show up.
True.
I'm pretty sure most of us will only unit-test when moving
existing applications to v2.0 and 'problems' like this will simply pass the
test runs.
I guess that's a good reason to run unit tests in the debugger as well
as not. At least once :)
I just finished a code review for a large number of windows forms
applications, you would be surprised of the number of applications failing
to obey this UI threading rule. The reactions (some) of the (professional)
developers goes like this: "Oh, it's probably the debugger" or "normal, this
thing is still in beta, right" or "MSFT broke my code" or "Never heard about
this and it's not documented" etc.


Yes - scary, isn't it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
OK, I didn't try to run in Release mode, any way I thoght it is the easy way
to do what I had done,
I don't access the UI components directly , I use a public methode , I still
don't think this is a bad think since I mad a public methosd, those public
methodes access the components. Why this is a bad thing.
I have one function read from USB target, as you know if target didn't
transmit the data the host will keep asking for this data, So all I tried to
do is to make this function run in another thread and before start it I start
a timer to kill it when time is out. this function call an out funtion whitch
print out the data recived from USB .
the out is a UI thread which caled from the USB thread,

"Jon Skeet [C# MVP]" wrote:
[Yosi] <Yo**@discussions.microsoft.com> wrote:
Since Microsoft Allowd such a bad thing, there are product doing this bad
think , It's is so bad that Microsoft don't alowd this any more , if this
true, Microsoft must inform all those companies about this , so they can
decide if to buy the new VS2005 or not, (if to continue use the bad usage or
to make new release with the fix..)
I think they should add an option to allowd this or not (my in security
option list).


No. It's not something that they've just disallowed - it's *always*
been dangerous, and it's always been talked about all over the place.
It's not like it's not documented. It's just that now in debug mode the
mistakes can be found more clearly.

Your code was broken under VS.NET 2003 - it just wasn't as *obviously*
broken.

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

Nov 16 '05 #9
[Yosi] <Yo**@discussions.microsoft.com> wrote:
OK, I didn't try to run in Release mode, any way I thoght it is the easy way
to do what I had done,
I don't access the UI components directly , I use a public methode , I still
don't think this is a bad think since I mad a public methosd, those public
methodes access the components. Why this is a bad thing.
But calling methods which access the components still means you end up
accessing the components from the wrong thread, doesn't it? That
violates the rules specified in the Control documentation.
I have one function read from USB target, as you know if target didn't
transmit the data the host will keep asking for this data, So all I tried to
do is to make this function run in another thread and before start it I start
a timer to kill it when time is out. this function call an out funtion whitch
print out the data recived from USB .
the out is a UI thread which caled from the USB thread,


Not sure what you mean by "the out". See
http://www.pobox.com/~skeet/csharp/t...winforms.shtml for how to
invoke a delegate on the UI thread from a different thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
You seem to confuse public methods with threads and I'm afraid you need to
spend some time to read about multithreading in a windows environment
a great place to start with is this: See
http://www.pobox.com/~skeet/csharp/t...winforms.shtml.
The rule simple "don't directly access UI elements from code running on a
thread that doesn't own the UI elements"
In your case, you call a "public method" from a non UI thread (your USB
thread), in this method you directly update the UI elements and THIS IS A
BAD thing in windows programming.

Willy.

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
OK, I didn't try to run in Release mode, any way I thoght it is the easy
way
to do what I had done,
I don't access the UI components directly , I use a public methode , I
still
don't think this is a bad think since I mad a public methosd, those public
methodes access the components. Why this is a bad thing.
I have one function read from USB target, as you know if target didn't
transmit the data the host will keep asking for this data, So all I tried
to
do is to make this function run in another thread and before start it I
start
a timer to kill it when time is out. this function call an out funtion
whitch
print out the data recived from USB .
the out is a UI thread which caled from the USB thread,

"Jon Skeet [C# MVP]" wrote:
[Yosi] <Yo**@discussions.microsoft.com> wrote:
> Since Microsoft Allowd such a bad thing, there are product doing this
> bad
> think , It's is so bad that Microsoft don't alowd this any more , if
> this
> true, Microsoft must inform all those companies about this , so they
> can
> decide if to buy the new VS2005 or not, (if to continue use the bad
> usage or
> to make new release with the fix..)
> I think they should add an option to allowd this or not (my in security
> option list).


No. It's not something that they've just disallowed - it's *always*
been dangerous, and it's always been talked about all over the place.
It's not like it's not documented. It's just that now in debug mode the
mistakes can be found more clearly.

Your code was broken under VS.NET 2003 - it just wasn't as *obviously*
broken.

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

Nov 16 '05 #11
Thanks

"Willy Denoyette [MVP]" wrote:
You seem to confuse public methods with threads and I'm afraid you need to
spend some time to read about multithreading in a windows environment
a great place to start with is this: See
http://www.pobox.com/~skeet/csharp/t...winforms.shtml.
The rule simple "don't directly access UI elements from code running on a
thread that doesn't own the UI elements"
In your case, you call a "public method" from a non UI thread (your USB
thread), in this method you directly update the UI elements and THIS IS A
BAD thing in windows programming.

Willy.

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
OK, I didn't try to run in Release mode, any way I thoght it is the easy
way
to do what I had done,
I don't access the UI components directly , I use a public methode , I
still
don't think this is a bad think since I mad a public methosd, those public
methodes access the components. Why this is a bad thing.
I have one function read from USB target, as you know if target didn't
transmit the data the host will keep asking for this data, So all I tried
to
do is to make this function run in another thread and before start it I
start
a timer to kill it when time is out. this function call an out funtion
whitch
print out the data recived from USB .
the out is a UI thread which caled from the USB thread,

"Jon Skeet [C# MVP]" wrote:
[Yosi] <Yo**@discussions.microsoft.com> wrote:
> Since Microsoft Allowd such a bad thing, there are product doing this
> bad
> think , It's is so bad that Microsoft don't alowd this any more , if
> this
> true, Microsoft must inform all those companies about this , so they
> can
> decide if to buy the new VS2005 or not, (if to continue use the bad
> usage or
> to make new release with the fix..)
> I think they should add an option to allowd this or not (my in security
> option list).

No. It's not something that they've just disallowed - it's *always*
been dangerous, and it's always been talked about all over the place.
It's not like it's not documented. It's just that now in debug mode the
mistakes can be found more clearly.

Your code was broken under VS.NET 2003 - it just wasn't as *obviously*
broken.

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


Nov 16 '05 #12
By the way, the reason they allow such code to run fine when it's outside
the debugger doesn't have anything to do with burning CPU cycles. It is to
make sure that existing code that has this problem works exactly as well as
it did with the previous release.

So any code that does this thing (which, as has been pointed out has always
been very bad - it's not that they allowed you to do it before, it's just
that it tended to crash in less obvious ways) will carry on working as
before outside of the debugger. But in the debugger it now tells you
straight away that there's a problem.

Since Microsoft Allowd such a bad thing,
They didn't. Code that did this breaks eventually today. In release mode
it will be no more or less unreliable with VS.NET 2005 than it was before.

there are product doing this bad think


Yes, and such products have a lot of problems. This hasn't changed in
VS.NET 2005 - products can still do this and will still have as many
problems as they did before when they run outside the debugger. And now, if
they happen to be in the debugger, they'll be told what they've done wrong.

So this is a case of the debugger doing a better job of telling you when
something is going wrong. This is a good thing.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/
Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
6
by: sathyashrayan | last post by:
Following are the selected thread from the date:30-jan-2005 to 31-jan-2005. I did not use any name because of the subject is important. You can get the original thread by typing the subject...
2
by: Benny | last post by:
I have a richtextbox that acts like a console with information on the status of the server I am creating. I spawn new threads for each connection that is created and would like to write the status...
4
by: Joe | last post by:
I'm hosting my web service on a Windows 2003 box which is remotely located. When trying to add a web reference to a C# project I get an error message 'There was an error downloading...
0
by: Mart | last post by:
Hi, I have just written (my first) VB.net app using MS Visual Basic 2005 Express Edition Beta. It is fairly simple, it reads some configuration data from an XML file then opens a new window...
0
by: pumaken68 | last post by:
Does anyone has experience with calling web service from pocket pc 2003 project in VS 2005 environment? I just migrated a smart device(pocket pc 2003) solution from VS 2003 to VS 2005. There are...
1
by: Stout | last post by:
Is it possible to create a trigger in one database, that after an insert, will update a database on a different server? If so, how would I do this? Thanks. Bill
0
by: fischermx | last post by:
Environment: Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) Microsoft .NET Framework Version 2.0.50727 Microsoft Visual Studio 2005 Professional Edition - ENU Service Pack...
0
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.