|
看了好幾個(gè)WinForm程序了,發(fā)現(xiàn)他們對(duì)進(jìn)度條的處理完全失去了進(jìn)度條的作用。他們都是采用Timer來(lái)處理,在線程結(jié)束的時(shí)候,直接賦值進(jìn)度條達(dá)到100%。和我以前做WebForm程序的時(shí)候完全不一樣,做WebForm程序的時(shí)候,進(jìn)度條是根據(jù)總體數(shù)據(jù)和每步執(zhí)行后而計(jì)算和更新的。在看了這幾個(gè)WinForm程序后,我在想:是否所有WinForm程序,在進(jìn)度條的處理上都不能保證實(shí)時(shí)進(jìn)度顯示?
其實(shí)用Timer來(lái)處理,不停的更新進(jìn)度條只是程序作者偷懶的方法。當(dāng)然這樣的好處就是可以簡(jiǎn)單化處理進(jìn)度條,代碼量少,不易出錯(cuò),調(diào)試方便。
還有一種方法,就是可以及時(shí)更新進(jìn)度條的數(shù)據(jù)的。那就是采用事件驅(qū)動(dòng)機(jī)制,在子線程中監(jiān)視復(fù)雜處理過(guò)程中的設(shè)定的事件,及時(shí)更新!直接看代碼: 程序代碼using System; using System.ComponentModel; using System.Windows.Forms; namespace WindowsApplication1 { /// <summary> /// Form1 類 /// </summary> public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //用子線程工作 new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start(); } //開(kāi)始下載 public void StartDownload() { Downloader downloader = new Downloader(); downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress); downloader.Start(); } //同步更新UI void downloader_onDownLoadProgress(long total, long current) { if (this.InvokeRequired) { this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current }); } else { this.progressBar1.Maximum = (int)total; this.progressBar1.Value = (int)current; } } }
/// <summary> /// 下載類(您的復(fù)雜處理類) /// </summary> public class Downloader { //委托 public delegate void dDownloadProgress(long total,long current); //事件 public event dDownloadProgress onDownLoadProgress; //開(kāi)始模擬工作 public void Start() { for (int i = 0; i < 100; i++) { if (onDownLoadProgress != null) onDownLoadProgress(100, i); System.Threading.Thread.Sleep(100); } } } }=================================ling======================================== delegate object dlExecuteQuery(); private void button1_Click(object sender, EventArgs e) { dlExecuteQuery de=new dlExecuteQuery(this.Query()); IAsyncResult ir = de.BeginInvoke(null, null);
Form f=new Form() f.ShowDialog(this); Application.DoEvents(); while (!ir.IsCompleted) { Application.DoEvents(); } object obj = de.EndInvoke(ir); f.Close(); }
private object Query() { //長(zhǎng)時(shí)間的操作 }
_______________________________________________________________________________________________________________________________________
private void btnCount_Click(object sender, EventArgs e)
{ label1.Visible=true; progressBar.Visible = true; progressBar.Minimum = 0; progressBar.Maximum = ds.Tables["表"].Rows.Count; progressBar.BackColor = Color.Green; for (int i = 0; i < ds.Tables["表"].Rows.Count; i++)  { progressBar.Value++; Application.DoEvents(); this.label1.Text = Convert.ToString(progressBar.Value); } }
或者
private void btnCount_Click(object sender, EventArgs e) { label1.Visible=true; progressBar.Visible = true; progressBar.Minimum = 0; progressBar.Maximum = ds.Tables["表"].Rows.Count; progressBar.BackColor = Color.Green; for (int i = 0; i < ds.Tables["表"].Rows.Count; i++) { progressBar.Value++; Application.DoEvents(); this.label1.Text = Convert.ToString(progressBar.Value);this.label1.Refresh(); }
} http://blog.163.com/light_warm/blog/static/3168104200861710226745/
另:
建立一個(gè)listBox將進(jìn)程名稱遍歷進(jìn)去
this.listBox1.Items.Clear(); Process[] MyProcesses=Process.GetProcesses(); foreach(Process MyProcess in MyProcesses) { this.listBox1.Items.Add(MyProcess.ProcessName); } this.listBox1.SelectedIndex=0;
選中l(wèi)istBox里面的項(xiàng)后將進(jìn)程詳細(xì)信息顯示在右面的Label中
try { string ProcessName=this.listBox1.Text; this.groupBox1.Text=ProcessName+"進(jìn)程的詳細(xì)信息"; Process[] MyProcess=Process.GetProcessesByName(ProcessName); this.label1.Text="進(jìn)程影象名:"+MyProcess[0].ProcessName; this.label2.Text="進(jìn)程ID:"+MyProcess[0].Id; this.label3.Text="啟動(dòng)線程樹(shù):"+MyProcess[0].Threads.Count.ToString(); this.label4.Text="CPU占用時(shí)間:"+MyProcess[0].TotalProcessorTime.ToString(); this.label5.Text="線程優(yōu)先級(jí):"+MyProcess[0].PriorityClass.ToString(); this.label6.Text="啟動(dòng)時(shí)間:"+MyProcess[0].StartTime.ToLongTimeString(); this.label7.Text="專用內(nèi)存:"+(MyProcess[0].PrivateMemorySize/1024).ToString()+"K"; this.label8.Text="峰值虛擬內(nèi)存:"+(MyProcess[0].PeakVirtualMemorySize/1024).ToString()+"K"; this.label9.Text="峰值分頁(yè)內(nèi)存:"+(MyProcess[0].PeakPagedMemorySize/1024).ToString()+"K"; this.label10.Text="分頁(yè)系統(tǒng)內(nèi)存:"+(MyProcess[0].PagedSystemMemorySize/1024).ToString()+"K"; this.label11.Text="分頁(yè)內(nèi)存:"+(MyProcess[0].PagedMemorySize/1024).ToString()+"K"; this.label12.Text="未分頁(yè)系統(tǒng)內(nèi)存:"+(MyProcess[0].NonpagedSystemMemorySize/1024).ToString()+"K"; this.label13.Text="物理內(nèi)存:"+(MyProcess[0].WorkingSet/1024).ToString()+"K"; this.label14.Text="虛擬內(nèi)存:"+(MyProcess[0].VirtualMemorySize/1024).ToString()+"K"; } catch(Exception Err) { MessageBox.Show("沒(méi)有此進(jìn)程,無(wú)法獲取信息!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //不處理異常 }
|