all groups > dotnet windows forms > may 2005 >
You're in the

dotnet windows forms

group:

Strange threading behavior


Strange threading behavior Jon Shemitz
5/6/2005 3:26:41 PM
dotnet windows forms: I'm experimenting with a WinForms peer-to-peer .NET Remoting project.
Each of the forms exports an object that can put messages in a
RichTextBox, using SelectionText. The remote "client" form calls its
proxied version of this object, to put text in the "server's"
RichTextBox.

Now, the remote requests do NOT run in the GUI thread - yet

RTF.SelectionStart = RTF.Text.Length;
RTF.SelectionColor = MessageColor;
RTF.SelectedText = Message;

does NOT cause cross-thread synchronization errors, as I'd expect it
to. Why? Is SelectedText special in some way?

Strangely, if I set Text in the GUI thread, subsequent non-GUI thread
sets of SelectedText do cause the errors I'd expect. If I use
SelectedText for the initial set, subsequent non-GUI thread sets of
SelectedText do not cause any errors.

2.0 Beta 2, if that makes any difference.

--

Re: Strange threading behavior Jon Shemitz
5/6/2005 11:24:42 PM
[quoted text, click to view]

Yeah, I know. That's why I'm so puzzled that it does work. In Release
mode, too.

I'm wondering if the rules have changed in beta 2.

--

Re: Strange threading behavior Jon Skeet [C# MVP]
5/7/2005 12:00:00 AM
[quoted text, click to view]

No, things have always worked to some extent when you use the wrong
thread - it's just that they're likely to go bang when you least expect
it.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Strange threading behavior Jon Skeet [C# MVP]
5/7/2005 12:00:00 AM
[quoted text, click to view]

Well, I suspect it's a detail of how the threading is checked in debug
mode - but it definitely doesn't mean it's *safe* to do that work in
the non-UI thread.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: Strange threading behavior Jon Shemitz
5/7/2005 12:15:08 AM
[quoted text, click to view]

Oh, OK. Thanks. That explains the way the errors seem to go away when
I change code in minor ways, and don't always come back when if I
change the code back.

I'll change all my server-side GUI access to use Invoke (or
BeginInvoke).

--

AddThis Social Bookmark Button