Hi, Can somebody please explain the following to me. If I try to change a UltraLabels Text or UltraButtons text and I have not checked to see if Invoke is required then I get an Error messages telling me so (Which is what I want when debugging so I can fix it) now what I cannot seem to make happen is an Cross Thread Error when I read an UltraLabels text or UltraButtons text. If I stop my code at a specific line where I know I'm going to read an objects text (or anything else) I can see that the text parameter says {"Cross Thread Operation not Valid.....} yet my code continues with no error.
It is my understanding that you must Invoke when ever you SET/GET any info from an object that was created on another thread.
Is this the correct behavior? or do I not have a setting for checking this activated? I can see that the CheckForIllegalCrossThreadCalls is set to true.
As the code continues with no error what is this actually doing to my program as it does seem to be reading the text correctly of the label or button?
Thank You
-Paul
Paul,
The InvalidOperationException that you see comes from accessing the Handle property of the Control and the Handle is accessed on both the get and set of the Text property. What is different is that when you get the Text there is logic in the getter of the WindowText property (which is set in the getter of the Text property) that creates a System.Windows.Forms.Control.MultithreadSafeCallScope object which sets a thread static flag on the control to indicate that the call is thread safe.
Note that the MSDN documentation for Control states that "only the following members are thread safe: BeginInvoke, EndInvoke, Invoke, InvokeRequired, and CreateGraphics if the handle for the control has already been created" so I would still treat the Text property as though it is not thread safe. Also even if the call to the getter will never throw an exception or fail, the value that is returned may not be the value that you expect. If the value can be set on another thread then it would be possible to access the value in two subsequent lines of code and have it return a different value each time.
I am curious how you see a Cross Thread error was raised since the getter of the Text Property should doesn't throw that exception in the testing that I have done. Is there another scenario that you are seeing the exception for? If not what version of the .NET framework are you targeting? I was using .NET 4.5 for my testing.
If you want to know why the implementation is as it is then it would be best to ask that question on a Microsoft forum as they may be able to answer questions regarding the intention behind the implementation.
Let me know if you have any questions with this matter.
Hi, Thank you for reply.
I have read all these before and alot more, most of the information is about updating another objects info not getting info. I was looking for a answer on something that I cannot found an answer to in any of the info I have read. My question was not generally directed at Infragistics products its just that I use them all the time. As this is a general forum I was hoping that somebody could help answer my question.
All I wanted to know was why is it that when I try to change a objects settings like Text I will get a Cross Thread error which I understand and while debugging I can fix, yet if I use something like - if fom1.label1.Text = "blah" (which is on another thread) I do not get an error message yet I can see in the debug info that a Cross Thread error was raised. My program continues to run with no error and the returned results is correct.
So my quesion is when reading another objects info on another thread why do I not get a Cross Thread error message like I do if trying to change a objects info?
It is correct that UI Controls in Windows Forms are not thread safe and you can see a list of the thread safe members in the Thread Safety section of System.Windows.Forms.Control on MSDN. As far as the behavior of the Text property, this is actually inherited from the Control class and the behavior that you are seeing is the same as what you would see if using a Microsoft Label control.
If you want a better understanding of threading then I recommend the following resources: