Deadlock with Control.Invoke

G

Guest

Hello,

I am developing a multithreaded application which contains a
System.Timers.Timer that executes a set of methods everytime its Elapsed
event fires. These methods perform calculations and use the Control.Invoke
method in a number of places to marshall calls onto the main (UI) thread in
order to update collections that user interface controls are bound to. Each
execution takes about 0.3 - 0.5 secs and there are a number of places where
Control.Invoke is used. I always use a reference to the applications main
form for the Invoke method.

My problem is that a number of times now I have found that the application
just locks up. At the moment I have just added a new windows form which
locks up in its Form.Load event handler, with the timer thread blocked on an
Invoke call. Neither of these methods holds any locks, nor do they share any
objects. The Invoked method does not begin execution, as there is a Trace
statement in the first line that is not reached. This is reproduable, and
will occur every time I run the application during its startup. I do not
understand what could be causing this, as I cannot see any reason for the
apparent deadlock... Surely the main UI thread should just finish processing
the form.Load handler and eventually pick up the Invoke call? The line the
Main UI thread blocks on (according to the VS debugger) is not always the
same, but it is always in the same section of code. If I comment out this
code, then it works fine. All this code is doing is initalising the text of
some labels from one of the collections that gets populated by the timer
thread, and the Invoke is for a method that updates a collection completely
unrelated to the one the form is using.

This has occured in the past and I have gotten around the problem by
changing the way the code works, but I now need to understand what is causing
it.

Many thanks,

David Grant
 

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