Click here to Skip to main content
15,937,012 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone! I am using a ListView control in report mode. Sometimes I need to update certain attributes of the items. The problem is, that after several updates (ListView_SetItem or ListView_SetItemText) the program becomes deadlocked. What can be a cause of such behaviour? It happens exactly, when I try to change the text of the item. Thanks!
Updated 3-Jun-11 4:17am
BillW33 3-Jun-11 10:37am    
Are you trying to set the text from outside the UI thread? If you aren't sure you can test the InvokeRequired property of the control. If it is true then you are trying to access it from a different thread.
Sergey Alexandrovich Kryukov 3-Jun-11 12:25pm    
How can you be sure it's deadlock, not some other reason for hanging? Which thread is hanging?

1 solution

As there has been no response from the OP as yet, I’m going to post a possible solution before I go offline.
A common problem when updating the UI is trying to do so from a thread other than the UI thread. If you run the program from inside Visual Studio it should catch this and let you know. However, when running the program outside of the debugger it may crash or lock up intermittently.
The old and simple way to solve this is through use of InvokeRequired. Every windows form control has the InvokeRequired Property. You can check InvokeRequired and if it is true marshal the call back onto the UI thread. This can be done as in the code below:
private void AddItem(string txt)
    if (InvokeRequired)
        Invoke((MethodInvoker)(() => AddItem (txt)));

See this link[^] for more details.

A better way is to use a SynchronizationContext. Code project has a 3 part series on this topic; see Part 1[^], Part 2[^] and Part 3[^].
Share this answer
Albert Holguin 4-Jun-11 12:38pm    
tag says C++

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900