C# WPF 性能提升


C# WPF性能提升总结

1. 提升list controls 的性能?

对于WPF里有两种方式 , 一个是数据虚拟化,一个是UI虚拟化。WPF 4.5支持UI虚拟化,但是只提供了部分控件的虚拟化,具体可以查阅相关资料。推荐http://blogs.msdn.com/b/dancre/archive/2006/02/16/implementing-a-virtualizingpanel-part-4-the-goods.aspx

2. 提升界面的相应速度?

将有大量计算的部分放到非UI线程里,等计算完后,回到主线程操作UI. 在某些场景下可以使用BackgourndWorker

http://www.codeproject.com/Articles/228869/BackgroundWorker-and-UI-threads

部分代码:

// Thread pool thread
m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
// UI thread
m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
// UI thread
m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);

3. 对于部分频繁的刷新操作,可以通过DispatcherTimer来定时处理?

http://www.codeproject.com/Articles/167365/All-about-NET-Timers-A-Comparison

在DispatcherTimer有一个属性, DispatcherPriority:

  • Inactive - throws an exception
  • SystemIdle - called when System is Idle or SystemIdle process takes maximum CPU time
  • ApplicationIdle - Processes when Application is idle means UI thread is free
  • ContextIdle - Processed when background operation is complete
  • Background - Processed in background means processed only when all other non-idle operations are complete
  • Send- Highest, and sent immediately

如果为了提高UI的响应性,我们应该选在ApplicationIdle.

4. 遇到大数据(图片,曲线)显示,如何提高性能?

这个我觉得是最难的部分,我们的界面的像素是有限的,比如我要画一条水平直线, UI控件的像素的宽度最大也就3000,数据给我们的可能是300,000 个点,这样可能一个像素点会对应100个数据点,我们只要画一次就够了,所以需要相应的降采样算法。注意显示端我们可以降采样,但是所有的计算操作都应该基于真实数据,而不是显示数据。

5. C#操作图像时,用unsafe 代码提高性能?

http://www.codeproject.com/Articles/1989/Image-Processing-for-Dummies-with-C-and-GDI-Part

代码实例:

public static bool Invert(Bitmap b)

{

// GDI+ still lies to us - the return format is BGR, NOT RGB.

BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),

ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;

System.IntPtr Scan0 = bmData.Scan0;

unsafe

{

byte * p = (byte *)(void *)Scan0;

int nOffset = stride - b.Width*3;

int nWidth = b.Width * 3;

for(int y=0;y < b.Height;++y)

{

for(int x=0; x < nWidth; ++x )

{

p[0] = (byte)(255-p[0]);

++p;

}

p += nOffset;

}

}

b.UnlockBits(bmData);

return true;

}

6. 使用并行库提高性能?

也就是充分利用多核来处理,在 WPF里要保证数据操作与UI操作的分离。因为UI操作只能在UI线程里操作,我们能利用多核的部分,只能是数据部分。还要保证这些数据的相对独立。不能互相影响,否则并行没有意义。

http://www.codeproject.com/Articles/152765/Task-Parallel-Library-of-n

7. ……

优质内容筛选与推荐>>
1、SQL Server 常用分页SQL(转)
2、Android探索之Service全面回顾及总结
3、Remove Duplicates from Sorted Array II
4、LPC43xx Dual-core or Multi-core configuration and JLink Debug
5、Java操作MySQL应用实例


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号