C# WinForm自定义添加控件,类似百度云PC端,内存占用厉害

cmflovekm 2016-09-27 11:19:21
C#自定义加载上传列表控件,类似百度云PC端,但是使用线程动态加载时出现内存占用明显,当数量超过100时效率明显下降,内存占用越来越大。
{
CurrentUpLoadList = MyXmlOp.GetLoadList(CurrentUser.UID);//这是获取的图片信息列表(很多1000+)
FormGetUpListThread = new Thread(new ThreadStart(ThreadFunc));
FormGetUpListThread.Start();
}

Thread FormGetUpListThread;
private void ThreadFunc()
{
int Count = CurrentUpLoadList.Count;
SetUpLoadListTh(Count);
}

private void SetUpLoadListTh(int Count)//内存占用比较厉害,所以限制最大数量为50
{
foreach(MyUpLoadInfo MyUp in CurrentUpLoadList)
{
if (MyUp.FileLength / 1024 < 10)
return;

//MyUpLoadControl MewControl = new MyUpLoadControl();//这个是封装的自定义控件,与下面代码生成的是一样的,效率也一至,也是一样存在内存占用问题
//MewControl.Dock = DockStyle.Top;
//MewControl.Tag = MyUp.FileFullName;
//Image Img = Image.FromFile(MyUp.FileFullName);
//MewControl.PicB.Image = new Bitmap(Img);
//Img.Dispose();
//AddListToUp(xtraTabPageUp, MewControl);

Panel nPanel = new Panel();
//DevExpress.XtraEditors.PanelControl nPanel = new DevExpress.XtraEditors.PanelControl();
nPanel.Height = 60;
nPanel.Width = this.xtraTabPageUp.Width - 20;
nPanel.Tag = "panel_" + MyUp.FileFullName;
nPanel.Dock = DockStyle.Top;
AddListToUp(xtraTabPageUp, nPanel);

Panel nPanelLine = new Panel();
nPanelLine.Dock = DockStyle.Top;
nPanelLine.Height = 1;
nPanelLine.BackColor = System.Drawing.Color.DarkGray;
//nPanelLine.Parent = nPanel;
AddListToUp(nPanel, nPanelLine);

PictureBox PicB = new PictureBox();
Image Img = Image.FromFile(MyUp.FileFullName);
PicB.Image = new Bitmap(Img);
Img.Dispose();
PicB.BackgroundImageLayout = ImageLayout.Zoom;
PicB.Size = new System.Drawing.Size(50, 50);
PicB.Location = new Point(10, 5);
AddListToUp(nPanel, PicB);

//System.Windows.Forms.ProgressBar pra = new System.Windows.Forms.ProgressBar();
DevExpress.XtraEditors.ProgressBar pra = new DevExpress.XtraEditors.ProgressBar();
pra.Tag = "pra_" + MyUp.FileFullName;
pra.ForeColor = System.Drawing.SystemColors.Highlight;
pra.Height = 20;
pra.Width = 220;
//pra.Parent = nPanel;
pra.Location = new Point(400, 20);
AddListToUp(nPanel, pra);


DevExpress.XtraEditors.LabelControl lbl = new DevExpress.XtraEditors.LabelControl();
//lbl.Parent = nPanel;
lbl.Tag = "labelUpName_" + MyUp.FileFullName;
lbl.Text = MyUp.FileName;
lbl.Location = new Point(80, 10);
AddListToUp(nPanel, lbl);

DevExpress.XtraEditors.LabelControl lbl1 = new DevExpress.XtraEditors.LabelControl();
//lbl1.Parent = nPanel;
lbl1.Tag = "lblTime_" + MyUp.FileFullName;
lbl1.Text = " --";
lbl1.Location = new Point(340, 22);
lbl1.ForeColor = System.Drawing.SystemColors.GrayText;
AddListToUp(nPanel, lbl1);

DevExpress.XtraEditors.LabelControl lbl2 = new DevExpress.XtraEditors.LabelControl();
//lbl2.Parent = nPanel;
lbl2.Tag = "lblSpeed_" + MyUp.FileFullName;
lbl2.Text = "0KB/S";
lbl2.Location = new Point(640, 22);
lbl2.ForeColor = System.Drawing.SystemColors.Highlight;
AddListToUp(nPanel, lbl2);

DevExpress.XtraEditors.LabelControl lbl3 = new DevExpress.XtraEditors.LabelControl();
//lbl3.Parent = nPanel;
lbl3.Tag = "lblState_" + MyUp.FileFullName;
lbl3.Text = MyUp.HasLoadLength / 1024 + "KB/" + MyUp.FileLength / 1024 + "KB";
lbl3.Location = new Point(80, 35);
lbl3.ForeColor = System.Drawing.SystemColors.GrayText;
AddListToUp(nPanel, lbl3);
}
}
private delegate void MyDelegateUI();
//List<string> DelegateNames = new List<string>();
void AddListToUp(Control Par,Control ctr)//若不使用委托,线程中奖无法对一些控件进行操作
{
if (xtraTabPageUp.InvokeRequired)
{
MyDelegateUI d = delegate
{
Par.Controls.Add(ctr);
};
Par.Invoke(d);
}
else
{
Par.Controls.Add(ctr);
}
}
...全文
225 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Justin-Liu 2016-09-28
  • 打赏
  • 举报
回复
需要Dispose的对象要在用完及时Dispose
jamsunyue 2016-09-28
  • 打赏
  • 举报
回复
1. 调用对象后要清空,如:Panel nPanel = new Panel() 之后要 nPanel = null; 2. 用 GC 的内存释放机制

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧