ListView.Invoke deadlocks within Asynchronous Delegate

M

Mike V

I have been struggling over this problem for the past few days. Here is
what's happening:

1) My main UI thread creates a ListView control.
2) My main UI thread asynchronously invokes a delegate
3) The Asynchronous Delegate function calls ListView.Invoke() to attempt to
invoke *another* delegate
4) ListView.Invoke() hangs. Its call stack shows it hanging at the
System.Threading.WaitHandle.WaitOne() method. Presumably, there is some
sort of deadlock, but what could be causing this?? I've tried with/without
ReaderWriterLocks, Sleep statements to eliminate race conditions, and they
make no difference in avoiding the deadlock.
 
B

Bruce Brown

Hi,

This is a common problem and sometimes it is not always obvious what is
going on. You seem to have a good grasp of the threading issues surrounding
windows UI programming. You just need to change ListView.Invoke() to
ListView.BeginInvoke() to make sure you avoid the deadlock that is occuring.

Regards,

- Bruce.
 
M

Mike V

I've tried BeginInvoke() and it blocked eventually also. I think I solved
the problem though. I was trying to call ListView.Invoke() from an
asynchronous delegate spawned from an event handler function
(menuitem_click), which is called on the main UI thread. So when I call
ListView.Invoke() from that worker thread, it presumably waits for the main
UI thread to become available for Invocation (I'm making up terms here.)
BUT, I was never exiting the event handler function (because I was polling
for the results from the ListView.Invoke() call), so the ListView.Invoke()
call could never get an opportunity to run.
 

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