C#使用委托調(diào)用實現(xiàn)用戶端等待閃屏
以前總在博客園看別人寫的博客,這是我第一次寫技術(shù)博客,竟然不知道如何開始。在此向博客園里各位辛勤耕耘的各位博主致敬。
我以前開發(fā)Asp.net 程序較多,少有接觸WinForm。最近調(diào)換了工作,也有機會接觸WinForm.首先做WinForm的感覺像是客場作戰(zhàn),好多東西都不大熟悉。所以要加強努力。
廢話少說,進入正題。首先說說場景:
程序開發(fā)難免會有大數(shù)據(jù)量操作,在操作大量數(shù)據(jù)時,有時候需用戶等待,在這一段時間內(nèi)既不想讓用戶點其它操作,又不像讓用戶感覺程序假死了。怎么辦?對,就是要需使用一個等待的閃屏,告訴用戶"數(shù)據(jù)讀取中"旁邊還有一個gif動畫在轉(zhuǎn)動。等到完成操作時,閃屏自動關(guān)閉。
接下來看看效果:
可能會有很多同學笑我了:這么簡單的東西,還拿出來寫?簡單是簡單了點兒,可是對于一個WinForm不熟悉的人來說卻也費了不少周章。
再接下來是實現(xiàn)方式
1、簡單的實體類。(PS:因為是個小Demo 這個實體就沒怎么加注釋,^_^)
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.ComponentModel;
- usingSystem.Collections;
- namespaceDemo
- {
- publicclassProduct
- {
- publicintProductID { set;get;}
- publicstringProductName { set;get;}
- publicintCount { set;get;}
- publicdoublePice { set;get;}
- publicstringUint { set;get;}
- }
- }
2、等待閃屏:相對簡單,沒有代碼。在窗體上拖了一個Lable控件 和一個PictureBox,把Lable的Text屬性設(shè)置為:“數(shù)據(jù)讀取中”并且改了一下字體樣式,給PictureBox裝載一個gif圖像
3、主窗體:在主窗體上拉個網(wǎng)格控件(本Demo使用Developer Express的網(wǎng)格控件)、一個按鈕:把按鈕的Text屬性改為 “讀取”、一個BindingSource,
下面看主窗體的實現(xiàn)代碼
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.ComponentModel;
- usingSystem.Data;
- usingSystem.Drawing;
- usingSystem.Text;
- usingSystem.Windows.Forms;
- usingDevExpress.XtraEditors;
- usingSystem.Data.Linq;
- usingSystem.Threading;
- namespacedevDemo
- {
- publicpartialclassFormMain : Form
- {
- publicFormMain()
- {
- InitializeComponent();
- }
- frmLoading loading = newfrmLoading();//閃屏窗體
- #region委托
- ///<summary>
- ///關(guān)閉閃屏///</summary>
- publicdelegatevoidCloseloading();
- ///<summary>
- ///綁定數(shù)據(jù)///</summary>
- ///<param name="ls">數(shù)據(jù)列表</param>
- publicdelegatevoidBindedData(List<Product> ls);
- #endregion
- privatevoidFormMain_Load(objectsender, EventArgs e)
- {
- }
- ///<summary>
- ///讀取按鈕點擊事件///</summary>
- privatevoidbutton1_Click(objectsender, EventArgs e)
- {
- newAction(ReadData).BeginInvoke(newAsyncCallback(CloseLoading), null);
- loading.ShowDialog();//顯示loading
- }
- ///<summary>
- ///讀取數(shù)據(jù)///</summary>
- publicvoidReadData()
- {
- List<Product> productList = newList<Product>();
- //裝載模擬數(shù)據(jù)
- for(inti = 0;i <15;i++)
- {
- productList.Add(newProduct
- {
- ProductID = i + 1,
- Count = newRandom().Next(i * 10, 3000/ (i + 1)),
- Pice = System.Math.Round(newRandom().NextDouble() * (i + 1) * 100, 4),
- Uint = "只",
- ProductName = string.Format("產(chǎn)品{0}", i)
- });
- Thread.Sleep(200);//每添加一條記錄休息200毫秒
- }
- this.Invoke(newBindedData((pls) => {
- //綁定數(shù)據(jù)
- this.protuctBindingSource.DataSource = pls;
- }),productList);
- }
- ///<summary>
- ///關(guān)閉loading///</summary>
- ///<param name="ar"></param>
- publicvoidCloseLoading(IAsyncResult ar)
- {
- this.Invoke(newCloseloading(() => { loading.Close(); }));
- }
- }
- }
至此這個Demo完成.若有不足之處,或是有更好的方式,歡迎提出。
另外,寫技術(shù)博客真不容易。佩服那些一直更新自己博客的老師們。
原文鏈接:http://www.cnblogs.com/james2010/archive/2011/12/21/2296531.html
【編輯推薦】