Pen*_*m10 2 c# mobile multithreading listview compact-framework
我有一个文本过滤器,在TextChanged事件中我以这种方式启动listview填充代码:
ThreadPool.QueueUserWorkItem(new WaitCallback(populate));
Run Code Online (Sandbox Code Playgroud)
然后在populate方法中我有这样的代码
listView1.BeginUpdate();
listView1.Clear();
// rest of the code
listView1.EndUpdate();
Run Code Online (Sandbox Code Playgroud)
但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:
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我想在后台线程中发出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委托.诚然,这可能在更新版本中有所改变.