ListView控件死锁 [英] ListView control deadlocked

查看:71
本文介绍了ListView控件死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我在报告模式下使用ListView控件.有时我需要更新项目的某些属性.问题是,在几次更新(ListView_SetItem或ListView_SetItemText)之后,程序将陷入死锁状态.造成这种行为的原因是什么?当我尝试更改项目的文本时,它确实发生了.

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!

推荐答案

由于尚未收到OP的回复,因此我将在离线之前发布可能的解决方案.
更新UI时的一个常见问题是尝试从UI线程以外的线程进行更新.如果您从Visual Studio内部运行该程序,它应该捕获并让您知道.但是,在调试器之外运行程序时,它可能会崩溃或间歇性地锁定.
解决此问题的古老而简单的方法是使用InvokeRequired.每个Windows窗体控件都具有InvokeRequired属性.您可以检查InvokeRequired,如果为true,则将回调编组到UI线程.可以按照下面的代码来完成:
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)));
        return;
    }
    ListView1.Items.Add(txt);
}



请参阅此链接 [第1部分 [第2部分 [ ^ ]和第3部分 [



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[^].


这篇关于ListView控件死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆