用C#开发的应用程序因为占用过多的CPU资源导致系统"假死" 又一个无解的问题,回答无非就是:优化系统啊,释放相关资源啊,多线程啊,DoubleBufered,双缓冲啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一个模块一个模块的跟踪 有可能出在控件重绘的事情上如果你有一张表,10万条数据,你点表格中间那个分格条,让它重新调重表格时就会感觉到,程序卡死当然合理设置表格和优化取数据源可以解决 尽量少用事件,有时程序大部分时间都去处理事件去了,比如表格的重绘事件,有些人加了行号,大量数据时,问题越多,可能你删除一行,刷新等等都会造成大量的CPU界面少放控件,少用timer,适当使用多线程可能帮不到你,但一个人一个思路,可能你会找到问题点,反正你一个模块一个模块的跟踪,看程序从开始到结束都执行了什么 多谢各位这么快就给了参考意见啊..关于"优化系统,释放相关资源"---我实在想不到好的方法,先打算把部分数据读取到内存里,这样减少访问数据库的次数.关于"有可能出在控件重绘的事情上",--- 界面刷新,是用画图的方式,采用双缓冲,SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);UpdateStyles();但是重写了OnPaint方法,,有可能重画,,但是次数也不多啊...关于"尽量少用事件,有时程序大部分时间都去处理事件去了,比如表格的重绘事件,有些人加了行号,大量数据时,问题越多,可能你删除一行,刷新等等都会造成大量的CPU"--- 我的程序里用事件是比较多,,但是少用事件,那怎么来转换哪些事件操作呢??关于"界面少放控件,少用timer,适当使用多线程"--- C/S模式的,,主界面就12个button,8个TabControl,用了一个timer,这应该不多吧,用的是多线程. B/S模式的,出现假死现象,一般就是在IIS建立新的独立的应用程序池,适当的设置下,将资源释放。我就是利用这种方法解决假死现象的 页面错误 最大的地方就是drawImage。。 li45214521:我也觉得是 画图处理部分所导致,,但是我确找不到根本的原因和解决办法啊..能否说教说教啊.呵呵先介绍介绍我程序内部的大致处理:画图集中在 Monitor.cs类里面,初始化的时候设置了双缓冲画图方式:SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);UpdateStyles();在运行过程 Monitor 类作为 观察者 来接收其他对象状态的变化:/// <summary>/// 重写通知状态变化/// </summary>/// <param name="state">状态数据</param>public override void SendNotify(object state){ ...}接收到状态变化后,就进入了画图,都是以下处理方式:#region " 主线订单号变化信息"/// <summary>/// 更新主线定单状态/// </summary>/// <param name="state"></param>private void UpdateMainLineOrderState(ArrayList state){ mainLineOrderQueue.Enqueue(state); if (deviceProcess != null) { deviceProcess.ChangeState(new StateChangedDelegate(DoUpdateMainLineOrderState)); }}/// <summary>/// 更新主线定单状态/// </summary>private void DoUpdateMainLineOrderState(){ MethodInvoker doDoUpdateMainLineOrderStateDelegate = new MethodInvoker(DoUpdateMainLineOrderStateDelegate); if (InvokeRequired) { Invoke(doDoUpdateMainLineOrderStateDelegate); } else { doDoUpdateMainLineOrderStateDelegate(); } }/// <summary>/// 更新主线定单状态/// </summary>private void DoUpdateMainLineOrderStateDelegate(){ ArrayList bdA = mainLineOrderQueue.Dequeue() as ArrayList; using (Graphics graphics = CreateGraphics()) { DrawMainLineOrderState(bdA, graphics); }}private void DrawMainLineOrderState(ArrayList state, Graphics graphics){ try { // state 所包含的项:第几位|值|画图区域|TruePic|FalsePic|className string pic = state[3].ToString(); Bitmap bmp = new Bitmap(pic); // Bitmap bmp = state[4] as Bitmap; Rectangle range = (Rectangle)state[2]; string className = state[5].ToString(); float x = range.X - 2; float y = range.Y ; graphics.SetClip(range); if (int.Parse(state[1].ToString()) > 0) { if (pic.Equals("")) { graphics.FillRectangle(new SolidBrush(BadColor), range); } else if (File.Exists(pic)) { bmp = new Bitmap(pic); bmp.MakeTransparent(Color.White); graphics.DrawImage(bmp, range); Font wordsFont = new Font (Font.FontFamily,8.0f); Brush wordsBrush = Brushes.Black; graphics.DrawString(state[1].ToString(), wordsFont,wordsBrush,x,y); } } else { if (bmp == null) { graphics.FillRectangle(new SolidBrush(GoodColor), range); } else { graphics.DrawImage(bmp, range); } } } catch(Exception ex) { MessageBox.Show(ex.Message + ex.ToString()); }}然后,Monitor 类/// <summary>/// 重写重画方法/// </summary>/// <param name="e">画图参数对象</param>protected override void OnPaint(PaintEventArgs e){ if (!Visible || !canUpdate) return; Graphics graphics = e.Graphics; Draw(graphics); base.OnPaint(e); }在 Draw(graphics)方法里面,调用DrawMainLineOrderState(graphics);如下所示. /// <summary>/// 画主线定单状态/// </summary>/// <param name="graphics">画图对象</param>private void DrawMainLineOrderState(Graphics graphics){ if (this.mainlineOrderCollection == null) return; for (int i = 0; i < mainlineOrderCollection.Length; i++) { if (mainlineOrderCollection[i] != null) { // storeState 所包含的项:第几位|值|画图区域|TruePic|FalsePic|className ArrayList storeState = new ArrayList(); storeState.Add(mainlineOrderCollection[i].No); storeState.Add(mainlineOrderCollection[i].SerialNo); storeState.Add(mainlineOrderCollection[i].Range); storeState.Add(mainlineOrderCollection[i].TruePic); storeState.Add(mainlineOrderCollection[i].FalsePic); storeState.Add(mainlineOrderCollection[i].ClassName); DrawMainLineOrderState(storeState, graphics); } }} #endregion 对于drawImage行数不要调用太多,比如你可以通过200ms更新一次方式,(当然是所以数据更新都在一起,)另外你的程序很乱,// state 所包含的项:第几位|值|画图区域|TruePic|FalsePic|classNamestring pic = state[3].ToString();Bitmap bmp = new Bitmap(pic);函数不能调用多次,最好调用一次 ,尽量减少调用次数 WPF窗口比例如何保持恒定? 点击exe安装包直接被360杀掉 ,还报威胁 怎么解决 能不能用字典表+委托替代switch? 关于toolstrip 水晶报表加载失败?? C#中字符串在网络上传输的编码方式 GridView1中如果有选择按钮,为什么就分页为无较?能否两全其美? 我想使用IList.RemoveAt移除一个一维的string数组的最后一个元素,并重新生成数组,请问该如何书写代码 写个蜘蛛程序,遇到一牛仿蜘蛛、采集结构. C#中从Excel表中导入数据到Acess数据库中 我改怎么写这个按钮事件呀?请给我个思维,谢谢 急需帮忙!网站登录密码加密方式突然发生变化,导致不能登录管理
当然合理设置表格和优化取数据源可以解决 尽量少用事件,有时程序大部分时间都去处理事件去了,比如表格的重绘事件,有些人加了行号,大量数据时,问题越多,可能你删除一行,刷新等等都会造成大量的CPU界面少放控件,少用timer,适当使用多线程可能帮不到你,但一个人一个思路,可能你会找到问题点,反正你一个模块一个模块的跟踪,看程序从开始到结束都执行了什么
---我实在想不到好的方法,先打算把部分数据读取到内存里,这样减少访问数据库的次数.关于"有可能出在控件重绘的事情上",
--- 界面刷新,是用画图的方式,采用双缓冲,
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
UpdateStyles();但是重写了OnPaint方法,,有可能重画,,但是次数也不多啊...关于"尽量少用事件,有时程序大部分时间都去处理事件去了,比如表格的重绘事件,有些人加了行号,大量数据时,问题越多,可能你删除一行,刷新等等都会造成大量的CPU"
--- 我的程序里用事件是比较多,,但是少用事件,那怎么来转换哪些事件操作呢??
关于"界面少放控件,少用timer,适当使用多线程"
--- C/S模式的,,主界面就12个button,8个TabControl,用了一个timer,这应该不多吧,用的是多线程.
li45214521:我也觉得是 画图处理部分所导致,,但是我确找不到根本的原因和解决办法啊..能否说教说教啊.呵呵先介绍介绍我程序内部的大致处理:画图集中在 Monitor.cs类里面,初始化的时候设置了双缓冲画图方式:
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
UpdateStyles();在运行过程 Monitor 类作为 观察者 来接收其他对象状态的变化:
/// <summary>
/// 重写通知状态变化
/// </summary>
/// <param name="state">状态数据</param>
public override void SendNotify(object state)
{
...
}接收到状态变化后,就进入了画图,都是以下处理方式:#region " 主线订单号变化信息"
/// <summary>
/// 更新主线定单状态
/// </summary>
/// <param name="state"></param>
private void UpdateMainLineOrderState(ArrayList state)
{
mainLineOrderQueue.Enqueue(state);
if (deviceProcess != null)
{
deviceProcess.ChangeState(new StateChangedDelegate(DoUpdateMainLineOrderState));
}
}
/// <summary>
/// 更新主线定单状态
/// </summary>
private void DoUpdateMainLineOrderState()
{
MethodInvoker doDoUpdateMainLineOrderStateDelegate = new MethodInvoker(DoUpdateMainLineOrderStateDelegate);
if (InvokeRequired)
{
Invoke(doDoUpdateMainLineOrderStateDelegate);
}
else
{
doDoUpdateMainLineOrderStateDelegate();
}
}/// <summary>
/// 更新主线定单状态
/// </summary>
private void DoUpdateMainLineOrderStateDelegate()
{
ArrayList bdA = mainLineOrderQueue.Dequeue() as ArrayList;
using (Graphics graphics = CreateGraphics())
{
DrawMainLineOrderState(bdA, graphics);
}
}private void DrawMainLineOrderState(ArrayList state, Graphics graphics)
{
try
{
// state 所包含的项:第几位|值|画图区域|TruePic|FalsePic|className
string pic = state[3].ToString();
Bitmap bmp = new Bitmap(pic);
// Bitmap bmp = state[4] as Bitmap;
Rectangle range = (Rectangle)state[2];
string className = state[5].ToString(); float x = range.X - 2;
float y = range.Y ;
graphics.SetClip(range); if (int.Parse(state[1].ToString()) > 0)
{
if (pic.Equals(""))
{
graphics.FillRectangle(new SolidBrush(BadColor), range);
}
else if (File.Exists(pic))
{
bmp = new Bitmap(pic);
bmp.MakeTransparent(Color.White);
graphics.DrawImage(bmp, range); Font wordsFont = new Font (Font.FontFamily,8.0f);
Brush wordsBrush = Brushes.Black; graphics.DrawString(state[1].ToString(), wordsFont,wordsBrush,x,y);
}
}
else
{
if (bmp == null)
{
graphics.FillRectangle(new SolidBrush(GoodColor), range);
}
else
{
graphics.DrawImage(bmp, range);
} }
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + ex.ToString());
}
}然后,Monitor 类
/// <summary>
/// 重写重画方法
/// </summary>
/// <param name="e">画图参数对象</param>
protected override void OnPaint(PaintEventArgs e)
{
if (!Visible || !canUpdate) return;
Graphics graphics = e.Graphics;
Draw(graphics);
base.OnPaint(e);
}在 Draw(graphics)方法里面,调用DrawMainLineOrderState(graphics);如下所示.
/// <summary>
/// 画主线定单状态
/// </summary>
/// <param name="graphics">画图对象</param>
private void DrawMainLineOrderState(Graphics graphics)
{
if (this.mainlineOrderCollection == null) return; for (int i = 0; i < mainlineOrderCollection.Length; i++)
{
if (mainlineOrderCollection[i] != null)
{
// storeState 所包含的项:第几位|值|画图区域|TruePic|FalsePic|className
ArrayList storeState = new ArrayList();
storeState.Add(mainlineOrderCollection[i].No);
storeState.Add(mainlineOrderCollection[i].SerialNo);
storeState.Add(mainlineOrderCollection[i].Range);
storeState.Add(mainlineOrderCollection[i].TruePic);
storeState.Add(mainlineOrderCollection[i].FalsePic);
storeState.Add(mainlineOrderCollection[i].ClassName);
DrawMainLineOrderState(storeState, graphics);
}
}
} #endregion
// state 所包含的项:第几位|值|画图区域|TruePic|FalsePic|className
string pic = state[3].ToString();
Bitmap bmp = new Bitmap(pic);函数不能调用多次,最好调用一次 ,尽量减少调用次数