我有一个文本过滤器,在 TextChanged 事件中我以这种方式启动列表视图填充代码:
ThreadPool.QueueUserWorkItem(new WaitCallback(populate));
然后在填充方法中我有这样的代码
listView1.BeginUpdate();
listView1.Clear();
// rest of the code
listView1.EndUpdate();
但 listView1.BeginUpdate() 调用给出以下异常:
System.NotSupportedException was unhandled
Message="An error message cannot be displayed because an optional resource assembly containing it cannot be found"
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Windows.Forms.ListView.ntvSetStyleEx()
at System.Windows.Forms.ListView.BeginUpdate()
at App.frmSelectClient.populate()
at WorkItem.doWork()
at System.Threading.Timer.ring()
InnerException:
我做错了什么?
我想在后台线程中发出 ListView 的数量。
您无法从 UI 线程以外的任何线程更新 UI 元素。使用 Control.Invoke/BeginInvoke 来执行此操作。
您可以在后台线程中完成所有数据加载等操作,但您需要编组到 UI 线程以实际填充 UI 控件本身。
大多数 UI 框架都是这种情况 - 当然还有 Windows Forms(桌面和 CF)和 Windows Presentation Foundation(您使用 Dispatcher 而不是 Control.Invoke/BeginInvoke)。
需要注意的一点是:如果我没记错的话,Compact Framework 仅支持 Control.Invoke/BeginInvoke 的 EventHandler 委托。诚然,这在最近的版本中可能已经改变。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)