使用线程和ListView C#中的问题 [英] A problem in c# using Threads and ListView

查看:315
本文介绍了使用线程和ListView C#中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本过滤其中TextChanged事件我推出一个ListView填充code是这样的:

I have a text filter where in the TextChanged event I launch a listview populate code this way:

ThreadPool.QueueUserWorkItem(new WaitCallback(populate));

然后在填充方法,我有code这样的

Then in the populate method I have code like this

listView1.BeginUpdate();
listView1.Clear();

// rest of the code

listView1.EndUpdate();

但listView1.BeginUpdate()调用给出了以下异常:

but the listView1.BeginUpdate() call gives the following exception:

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的填充后台线程。

I would like to issue the populate of the ListView in a background thread.

推荐答案

您不能更新从非UI线程以外的任何线程UI元素。使用Control.Invoke / BeginInvoke的做到这一点。

You can't update UI elements from any thread other than the UI thread. Use Control.Invoke/BeginInvoke to do this.

您可以做你的所有数据加载等在后台线程,但是你需要元帅到UI线程实际填充UI控件本身。

You can do all your data loading etc in the background thread, but you'll need to marshal to the UI thread to actually populate the UI controls themselves.

这是在大多数的UI框架的情况 - 当然Windows窗体(台式机和CF)和Windows presentation基金会(您使用调度,而不是Control.Invoke /的BeginInvoke)

This is the case in most UI frameworks - and certainly Windows Forms (desktop and CF) and Windows Presentation Foundation (where you use a Dispatcher rather than Control.Invoke/BeginInvoke).

要注意的一点:如果我没记错的话,Compact Framework的只支持事件处理程序委托Control.Invoke / BeginInvoke的。这可能在新的版本已经改变,无可否认。

One point to note: if I remember rightly, the Compact Framework only supports the EventHandler delegate for Control.Invoke/BeginInvoke. That may have changed in more recent versions, admittedly.

这篇关于使用线程和ListView C#中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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