Strange threading behavior

J

Jon Shemitz

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.
 
J

Jon Skeet [C# MVP]

Jon Shemitz said:
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.

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.
 
J

Jon Shemitz

Jon Skeet said:
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.

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.
 
J

Jon Skeet [C# MVP]

Jon Shemitz said:
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.

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.
 
J

Jon Shemitz

Jon Skeet said:
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.

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top