all groups > dotnet windows forms > august 2007 >
You're in the

dotnet windows forms

group:

Deadlock encountered during call to Invoke


Deadlock encountered during call to Invoke Flack
8/29/2007 12:00:02 PM
dotnet windows forms:
Hey guys,

I'm getting into a deadlock condition every once in a while when running my
app. When I see that it's deadlocked I "Break All" in the VS debugger and
take a look at what the current running threads are doing. I noticed that
one of my threads (the one I expect to be running at the time) is waiting in
a call to Invoke. Here is the stack trace:

[quoted text, click to view]


I see that the Invoke method is waiting after a call to WaitOne. Can
someone explain to me what exactly it's waiting on?

Lets say the call was Invoke(new MethodInvoker(OnUpdateStarted));
It didn't start running OnUpdateStarted yet since I would see some logging
indicating that it started.

Any tips on how to figure out what it's waiting on?
Also, when I use BeginInvoke, I do not see the problem (at least not yet).

Re: Deadlock encountered during call to Invoke Jon Skeet [C# MVP]
8/29/2007 8:58:43 PM
[quoted text, click to view]

When you call Invoke, that waits until the UI thread has executed the
delegate before the worker thread continues.

You may have a deadlock if your worker thread is holding a lock which
the UI thread needs to acquire before it can finish executing your
delegate.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Deadlock encountered during call to Invoke richie5um NO[at]SPAM googlemail.com
8/30/2007 3:05:03 AM
Hi,

This may or may not be your problem, but it sounds related to
something I encountered awhile ago. Basically I was seeing exactly
the same lock-up issue, even though I had correctly used Invoke/
BeginInvoke to handle all of the cross-thread UI functionality. On my
application I had a background thread that was updating the UI
components.

After a lot of head scratching I eventually found the problem -
because the handles are lazy created you have to be very careful with
when they get created, as they assume the thread ID of the calling
process, and not the original UI thread. Hence, if you call
"InvokeRequired" on a handle *before* that handle has been created, it
actually gets created within that call. And even though you have
correctly used it, the InvokeRequired will not only associate the
Handle with the wrong thread, but will return "false", even though
this is not the UI thread.

In the end, the simple solution was to call "IntPtr intPtr =
control.Handle;" immediately after I had created the control, thus
forcing its creation on the correct thread.

I then wrote a number of helpermethods to wrap the InvokeRequired
functionality behind single static helper methods - I put these into a
common module that I then reuse over all of my applications, e.g. ( in
this example you would call "UpdateControlText( yourControl,
yourText )" ):
private delegate void DelegateUpdateControlText( Control
_control, string _strText );

public static void UpdateControlText( Control _control, string
_strText )
{
if ( true != _control.InvokeRequired )
{
PerformUpdateControlText( _control, _strText );
}
else
{
_control.Invoke( new
DelegateUpdateControlText( ControlHelper.PerformUpdateControlText ),
new object[] { _control, _strText } );
}
}

private static void PerformUpdateControlText( Control
_control, string _strText )
{
_control.Text = _strText;
}


regards,
RichS
RE: Deadlock encountered during call to Invoke Flack
8/30/2007 11:20:02 AM
Thanks for the answers. I'm still not clear on what is happening though. The
UI thread is running fine and responding. I can use my app as normal. It's
just that the form that the Invoke call was made on doesn't ever start the
delegate that the Invoke call was called with.

Can this happen? That the Invoke call is waiting not on the UI thread but
on something else or am I missing something?

RE: Deadlock encountered during call to Invoke Jon Skeet [C# MVP]
8/30/2007 8:08:39 PM
[quoted text, click to view]

Sounds odd to me.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button