看了好幾個WinForm程序了,發(fā)現(xiàn)他們對進度條的處理完全失去了進度條的作用。他們都是采用Timer來處理,在線程結(jié)束的時候,直接賦值進度條達到100%。和我以前做WebForm程序的時候完全不一樣,做WebForm程序的時候,進度條是根據(jù)總體數(shù)據(jù)和每步執(zhí)行后而計算和更新的。在看了這幾個WinForm程序后,我在想:是否所有WinForm程序,在進度條的處理上都不能保證實時進度顯示?
其實用Timer來處理,不停的更新進度條只是程序作者偷懶的方法。當然這樣的好處就是可以簡單化處理進度條,代碼量少,不易出錯,調(diào)試方便。
還有一種方法,就是可以及時更新進度條的數(shù)據(jù)的。那就是采用事件驅(qū)動機制,在子線程中監(jiān)視復(fù)雜處理過程中的設(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(); } //開始下載 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; //開始模擬工作 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() { //長時間的操作 }
_______________________________________________________________________________________________________________________________________
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/
另:
建立一個listBox將進程名稱遍歷進去
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里面的項后將進程詳細信息顯示在右面的Label中
try { string ProcessName=this.listBox1.Text; this.groupBox1.Text=ProcessName+"進程的詳細信息"; Process[] MyProcess=Process.GetProcessesByName(ProcessName); this.label1.Text="進程影象名:"+MyProcess[0].ProcessName; this.label2.Text="進程ID:"+MyProcess[0].Id; this.label3.Text="啟動線程樹:"+MyProcess[0].Threads.Count.ToString(); this.label4.Text="CPU占用時間:"+MyProcess[0].TotalProcessorTime.ToString(); this.label5.Text="線程優(yōu)先級:"+MyProcess[0].PriorityClass.ToString(); this.label6.Text="啟動時間:"+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="峰值分頁內(nèi)存:"+(MyProcess[0].PeakPagedMemorySize/1024).ToString()+"K"; this.label10.Text="分頁系統(tǒng)內(nèi)存:"+(MyProcess[0].PagedSystemMemorySize/1024).ToString()+"K"; this.label11.Text="分頁內(nèi)存:"+(MyProcess[0].PagedMemorySize/1024).ToString()+"K"; this.label12.Text="未分頁系統(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("沒有此進程,無法獲取信息!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //不處理異常 }
|