deadlock issue

  • Thread starter Thread starter Stephan Steiner
  • Start date Start date
S

Stephan Steiner

Hi

I think I have a deadlock issue that I can't seem to avoid. Basically, I
have a service firing remote events, and an event sink on the same machine
processing those events. The remote events are no problem, they arrive just
fine. However, then my communicator class sends the events to my GUI class,
which is supposed to process them. I have two events, one that results in
the application icon to be changed (and works just fine), and one that's
supposed to add a text line to a RichTextBox of my application.
While the event is properly fired by the communicator class and properly
received by the GUI class, the GUI class class can't process it. If I have
the GUI class just dump the contents of the event to the commandline, things
work just fine. However, if I want to add the text received with the event
to my RichTextBox via RichTextBox.AppendText(), then my application stops
responding for good.

So far I'm not using any threading so it can't be that the event is fired
from another thread context, but just for the heck of it, I created an
additional method in the GUI that does just the GUI update, and have my
event receiver call it via myGuiClass.Invoke(new
EventListener(GUIUpdateMethod)), but that doesn't help either.

Any ideas?

Regards
Stephan
 
Hi, Stephan

what is the code for updating RTBox? By description you have problem there.
You can checks which thread you are on using Console.WriteLine or
Debug.WriteLine and dumping current thread hash - see Thread.GetHashCode.
Most probably you are having problem when trying to change RTBox from non-UI
thread, which might happen if you use asynchronous BeginInvoke or one of
similar methods.

HTH
Alex
 
Alex
what is the code for updating RTBox? By description you have problem there.
You can checks which thread you are on using Console.WriteLine or
Debug.WriteLine and dumping current thread hash - see Thread.GetHashCode.
Most probably you are having problem when trying to change RTBox from non-UI
thread, which might happen if you use asynchronous BeginInvoke or one of
similar methods.

The code is really simple, it's

log.AppendText(line);

where log is my RTF textbox and line is a string. When I debug the program,
once the event to update the textbox is received, I've tried to look at the
properties of my textbox, and I get an "error: cannot obtain value" for the
Text property, which also has me concerned. It seems almost as if my textbox
is inacessible from the context I'm trying to access it, which makes no
sense to me whatsoever.

As for the Threads you asked about, I don't create any threads on my own,
however, if I get the thread hash as you suggested during the setup of my
GUI, the hash is different from what I get when I catch my event. Now I
guess I need a way to yield control to the GUI thread for the update. Any
idea how this can be achieved (I'm not using any Invoke code at all.. all
that is asynchronous is the event itself but that's the nature of events,
isn't it?)

Regards
Stephan
 
Urgh, now I do feel stupid. Knowing that I had a similar deadlock issue
about a year ago, I looked at how I solved that and tried the same in my
current program (as described in my first message). However, I missed one
small but important detail: I tried an invoke on this rather than my
RichTextBox. Having had another look at my earlier code, I finally figured
this out, and upon changing it, things are fine.

In case anyone ever runs into the same problem, here's a nice description of
a solution:
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx

Regards
Stephan
 
Stephan Steiner said:
Urgh, now I do feel stupid. Knowing that I had a similar deadlock issue
about a year ago, I looked at how I solved that and tried the same in my
current program (as described in my first message). However, I missed one
small but important detail: I tried an invoke on this rather than my
RichTextBox. Having had another look at my earlier code, I finally figured
this out, and upon changing it, things are fine.

If "this" is a form, then it should be okay - unless you created the
RichTextBox on a different thread than you created the form, which
sounds unusual.
 
If "this" is a form, then it should be okay - unless you created the
RichTextBox on a different thread than you created the form, which
sounds unusual.

"this" is a form, so I was a bit puzzled why it didn't work, but I figured
why complain if I found a working solution.

Stephan
 
Stephan Steiner said:
"this" is a form, so I was a bit puzzled why it didn't work, but I figured
why complain if I found a working solution.

It's always useful to find out what's going on. If you have the time,
could you have a look at whether the form is created on the same thread
as the RTB or not, and which thread is being used for the invocation
depending on whether you call rtb.Invoke or this.Invoke?
 
It's always useful to find out what's going on. If you have the time,
could you have a look at whether the form is created on the same thread
as the RTB or not, and which thread is being used for the invocation
depending on whether you call rtb.Invoke or this.Invoke?

The creation thread must be the same as I'm using the standard GUI
initialization created by the VS.NET GUI designer. I added a line to my
eventhandler for the Invoke command that spits out the thread hash, as it
turns out it's the same and now interestingly both types of Invoke work
just fine. I'm afraid I have no idea what changed in between my unsuccessful
attempts yesterday and today :(

Just out of curiosity, is there a difference in terms of performance /
complexity of the operations that go on behind the scenes in between calling
Invoke on my Form or my RTB?

Regards
Stephan
 
Stephan Steiner said:
The creation thread must be the same as I'm using the standard GUI
initialization created by the VS.NET GUI designer. I added a line to my
eventhandler for the Invoke command that spits out the thread hash, as it
turns out it's the same and now interestingly both types of Invoke work
just fine. I'm afraid I have no idea what changed in between my unsuccessful
attempts yesterday and today :(

Right - well, at least that means things aren't as strange as they
sounded :)
Just out of curiosity, is there a difference in terms of performance /
complexity of the operations that go on behind the scenes in between calling
Invoke on my Form or my RTB?

Pass, I'm afraid. I would expect them to be roughly the same, but it
could be that calling it on the RTB would end up walking up the control
hierarchy until it found the form. I wouldn't like to say anything for
sure though.
 
Back
Top