龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C#开发 >

C#使用委托调用实现用户端等待闪屏

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
以前总在博客园看别人写的博客,这是我第一次写技术博客,竟然不知道如何开始。在此向博客园里各位辛勤耕耘的各位博主致敬。 我以前开发Asp.net 程序较多,少有接触WinForm。最近调

以前总在博客园看别人写的博客,这是我第一次写技术博客,竟然不知道如何开始。在此向博客园里各位辛勤耕耘的各位博主致敬。

我以前开发Asp.net 程序较多,少有接触WinForm。最近调换了工作,也有机会接触WinForm.首先做WinForm的感觉像是客场作战,好多东西都不大熟悉。所以要加强努力。

废话少说,进入正题。首先说说场景:

程序开发难免会有大数据量操作,在操作大量数据时,有时候需用户等待,在这一段时间内既不想让用户点其它操作,又不像让用户感觉程序假死了。怎么办?对,就是要需使用一个等待的闪屏,告诉用户"数据读取中"旁边还有一个gif动画在转动。等到完成操作时,闪屏自动关闭。

接下来看看效果:

可能会有很多同学笑我了:这么简单的东西,还拿出来写?简单是简单了点儿,可是对于一个WinForm不熟悉的人来说却也费了不少周章。

再接下来是实现方式

1、简单的实体类。(PS:因为是个小Demo 这个实体就没怎么加注释,^_^)

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.ComponentModel;  
  6. usingSystem.Collections;  
  7. namespaceDemo  
  8. {  
  9. publicclassProduct  
  10. {  
  11.  publicintProductID { set;get;}  
  12. publicstringProductName { set;get;}  
  13. publicintCount { set;get;}  
  14. publicdoublePice { set;get;}  
  15. publicstringUint { set;get;}  
  16. }  

2、等待闪屏:相对简单,没有代码。在窗体上拖了一个Lable控件 和一个PictureBox,把Lable的Text属性设置为:“数据读取中”并且改了一下字体样式,给PictureBox装载一个gif图像

3、主窗体:在主窗体上拉个网格控件(本Demo使用Developer Express的网格控件)、一个按钮:把按钮的Text属性改为 “读取”、一个BindingSource,

下面看主窗体的实现代码

  1. usingSystem;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.ComponentModel;  
  4. usingSystem.Data;  
  5. usingSystem.Drawing;  
  6. usingSystem.Text;  
  7. usingSystem.Windows.Forms;  
  8. usingDevExpress.XtraEditors;  
  9. usingSystem.Data.Linq;  
  10. usingSystem.Threading;  
  11. namespacedevDemo  
  12. {  
  13. publicpartialclassFormMain : Form  
  14. {  
  15. publicFormMain()  
  16. {  
  17. InitializeComponent();  
  18. }  
  19. frmLoading loading = newfrmLoading();//闪屏窗体  
  20. #region委托  
  21. ///<summary>  
  22. ///关闭闪屏///</summary>  
  23.  publicdelegatevoidCloseloading();  
  24. ///<summary>  
  25. ///绑定数据///</summary>  
  26. ///<param name="ls">数据列表</param>  
  27.  publicdelegatevoidBindedData(List<Product> ls);  
  28. #endregion  
  29. privatevoidFormMain_Load(objectsender, EventArgs e)  
  30. {  
  31. }  
  32. ///<summary>  
  33. ///读取按钮点击事件///</summary>  
  34.  privatevoidbutton1_Click(objectsender, EventArgs e)  
  35. {  
  36. newAction(ReadData).BeginInvoke(newAsyncCallback(CloseLoading), null);  
  37. loading.ShowDialog();//显示loading  
  38. }  
  39. ///<summary>  
  40. ///读取数据///</summary>  
  41. publicvoidReadData()  
  42. {  
  43. List<Product> productList = newList<Product>();  
  44. //装载模拟数据  
  45. for(inti = 0;i <15;i++)  
  46. {  
  47. productList.Add(newProduct  
  48. {  
  49. ProductID = i + 1,  
  50. Count = newRandom().Next(i * 10, 3000/ (i + 1)),  
  51. Pice = System.Math.Round(newRandom().NextDouble() * (i + 1) * 100, 4),  
  52. Uint = "只",  
  53. ProductName = string.Format("产品{0}", i)  
  54. });  
  55.  Thread.Sleep(200);//每添加一条记录休息200毫秒  
  56. }  
  57. this.Invoke(newBindedData((pls) => {  
  58. //绑定数据  
  59.  this.protuctBindingSource.DataSource = pls;  
  60. }),productList);  
  61. }  
  62. ///<summary>  
  63. ///关闭loading///</summary>  
  64. ///<param name="ar"></param>  
  65. publicvoidCloseLoading(IAsyncResult ar)  
  66. {  
  67. this.Invoke(newCloseloading(() => { loading.Close(); }));  
  68. }  
  69. }  

至此这个Demo完成.若有不足之处,或是有更好的方式,欢迎提出。

另外,写技术博客真不容易。佩服那些一直更新自己博客的老师们。

http://www.cnblogs.com/james2010/archive/2011/12/21/2296531.html


精彩图集

赞助商链接