我的项目使用BackgroundThread加载列表视图中的字符串hgh列表,它的性能非常快,但需要将近100%cpu有没有任何解决方法可以避免高CPU使用率性能?我用netframework 3.5如何避免阅读大文件时CPU使用率过高
代码:提前
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)核心(TM)i3-2328M CPU @ 2.20GHZ 2.11 GHZ 安装的内存:1.00 GB 系统类型:32位操作系统系统
的文件有35939 KB大小与1个百万行
什么是您的系统配置,您拥有多少个逻辑处理器? CPU保持100%的时间?文件有多大,包含多少行? –
@SriramSakthivel @SriramSakthivel我不认为文件的大小很重要,所有重要的是它是巨大的:)但mysticgirltk,请写下你的系统配置,你的操作系统,操作系统和CPU的架构,内存大小和CPU时钟时间 – niceman
@niceman巨大是一个广泛的术语。巨大的巨大?你能回答这个问题吗?可能不会。所以最好避免混淆我问文件大小。 –