【发布时间】:2015-02-28 19:45:03
【问题描述】:
我的项目使用 BackgroundThread 将字符串的休长列表加载到列表视图中,它的性能非常快,但占用了近 100% 的 cpu 是否有任何解决方法可以在不降低其性能的情况下避免高 cpu 使用率?我使用网络框架 3.5
代码:
void readfile() {
deg_readfile read = new deg_readfile(readfileasync);
read.BeginInvoke(new AsyncCallback(callback),null);
}
void readfileasync(){
string[] strs = File.ReadAllLines("hughlist.txt");
List<ListViewItem> _itemlst = new List<ListViewItem>();
ListViewItem _item;
int x = 0;
//problem start here
foreach (string str in strs) {
_item = new ListViewItem(str);
for (int i = 0; i < 6; i++) {
_item.SubItems.Add("");
}
_itemlst.Add(_item);
x++;
_item = null;
}
}
void callback(IAsyncResult ae) {
Console.WriteLine("finished");
}
delegate void deg_readfile();
static void Main(string[] args)
{
Program prog = new Program();
Thread t=new Thread(new ThreadStart(prog.readfile))
{
IsBackground = true,
Priority = ThreadPriority.Lowest
};
t.SetApartmentState(ApartmentState.MTA);
t.Start();
Console.Read();
}
提前致谢。
更新:
系统配置:
处理器:Intel(R) Core(TM) i3-2328M CPU @ 2.20GHZ 2.11 GHZ 安装内存:1.00 GB 系统类型:32位操作系统
文件大小为 35,939 KB,包含 100 万行
【问题讨论】:
-
您的系统配置是什么,您有多少个逻辑处理器? CPU 保持 100% 多长时间?文件有多大,它包含多少行?
-
@SriramSakthivel 我不认为文件的大小很重要,重要的是它很大:),但是 mysticgirltk ,请写下你的系统配置,你的操作系统、操作系统和 CPU 的架构是什么、内存大小和 CPU 时钟时间
-
@niceman 巨大是一个广义的术语。巨大有多大?你能回答这个问题吗?可能不是。所以为了避免混淆,我询问了文件大小。
-
@SriramSakthivel ,也是 niceman ,感谢您的回复,我已经更新了问题,文件大小为 35,939 kb,我有 intel i core 3
标签: c# listview asynchronous foreach large-data