C#使用委托調(diào)用實(shí)現(xiàn)用戶端等待閃屏
以前總在博客園看別人寫的博客,這是我第一次寫技術(shù)博客,竟然不知道如何開始。在此向博客園里各位辛勤耕耘的各位博主致敬。
我以前開發(fā)Asp.net 程序較多,少有接觸WinForm。最近調(diào)換了工作,也有機(jī)會(huì)接觸WinForm.首先做WinForm的感覺像是客場作戰(zhàn),好多東西都不大熟悉。所以要加強(qiáng)努力。
廢話少說,進(jìn)入正題。首先說說場景:
程序開發(fā)難免會(huì)有大數(shù)據(jù)量操作,在操作大量數(shù)據(jù)時(shí),有時(shí)候需用戶等待,在這一段時(shí)間內(nèi)既不想讓用戶點(diǎn)其它操作,又不像讓用戶感覺程序假死了。怎么辦?對,就是要需使用一個(gè)等待的閃屏,告訴用戶"數(shù)據(jù)讀取中"旁邊還有一個(gè)gif動(dòng)畫在轉(zhuǎn)動(dòng)。等到完成操作時(shí),閃屏自動(dòng)關(guān)閉。
接下來看看效果:
可能會(huì)有很多同學(xué)笑我了:這么簡單的東西,還拿出來寫?簡單是簡單了點(diǎn)兒,可是對于一個(gè)WinForm不熟悉的人來說卻也費(fèi)了不少周章。
再接下來是實(shí)現(xiàn)方式
1、簡單的實(shí)體類。(PS:因?yàn)槭莻€(gè)小Demo 這個(gè)實(shí)體就沒怎么加注釋,^_^)
- 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、等待閃屏:相對簡單,沒有代碼。在窗體上拖了一個(gè)Lable控件 和一個(gè)PictureBox,把Lable的Text屬性設(shè)置為:“數(shù)據(jù)讀取中”并且改了一下字體樣式,給PictureBox裝載一個(gè)gif圖像
3、主窗體:在主窗體上拉個(gè)網(wǎng)格控件(本Demo使用Developer Express的網(wǎng)格控件)、一個(gè)按鈕:把按鈕的Text屬性改為 “讀取”、一個(gè)BindingSource,
下面看主窗體的實(shí)現(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>
- ///讀取按鈕點(diǎn)擊事件///</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(); }));
- }
- }
- }
至此這個(gè)Demo完成.若有不足之處,或是有更好的方式,歡迎提出。
另外,寫技術(shù)博客真不容易。佩服那些一直更新自己博客的老師們。
原文鏈接:http://www.cnblogs.com/james2010/archive/2011/12/21/2296531.html
【編輯推薦】