Windows Forms and Threads.

09/20/2011 11:13 BaussHacker#1
I know it's not a conquer related question, but since a lot of people here is C# programmers I just thought I would ask here.

Why is it exactly you cannot enter Windows Form Controls from another thread?

Not asking how to do it, but why it's not allowed.
09/20/2011 16:05 IAmHawtness#2
No idea, I think it's to prevent data corruption and/or thread locking

Edit:
Quote:
Originally Posted by http://msdn.microsoft.com/en-us/library/ms171728%28v=vs.80%29.aspx
Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.
So, it's not about data corruption anyways. I think it's pretty stupid though, I mean adding an item to a listbox or changing the title of a label in a seperate thread shouldn't be a problem if you know what you're doing - I'm pretty sure those things just use the PostMessage (or SendMessage) function anyways which is completely thread-safe. Microsoft is shit.
09/20/2011 21:26 pro4never#3
To ever so slightly expand on that... You can just turn off checking for cross thread calls... Or use super simple thread safe code to completely bypass the issue (and I guess technically make it less errorprone)
09/20/2011 22:22 IAmHawtness#4
Quote:
Originally Posted by pro4never View Post
To ever so slightly expand on that... You can just turn off checking for cross thread calls... Or use super simple thread safe code to completely bypass the issue (and I guess technically make it less errorprone)
I'd rather just use Control.(Begin)Invoke methods to be honest, it's probably the best/safest way. I'm pretty sure the thread starter already knows about these methods though, since he said he wasn't asking how to do it :p
09/21/2011 03:45 pro4never#5
Quote:
Originally Posted by IAmHawtness View Post
I'd rather just use Control.(Begin)Invoke methods to be honest, it's probably the best/safest way. I'm pretty sure the thread starter already knows about these methods though, since he said he wasn't asking how to do it :p
Yah I started to actually look into and use thread safe ways of accessing controls but just figured I'd mention as always that you can just turn the damn thing off.
09/21/2011 07:31 BaussHacker#6
Thanks for the answers both ^^
09/21/2011 14:25 InfamousNoone#7
[Only registered and activated users can see links. Click Here To Register...]

Set to false, if you really want to disable it.
09/21/2011 14:54 BaussHacker#8
Quote:
Originally Posted by InfamousNoone View Post
[Only registered and activated users can see links. Click Here To Register...]

Set to false, if you really want to disable it.
LOL! Well just wanted to know why it's not allowed, but didn't know that existed. :D