c#進度條案例

進度條案例

1:遞歸查詢目錄,包含子目錄

2:採用委託方案與form窗體交互。

3:此案例可擴展到:複製、移動、批量命名、或查詢等對象文件的操作。


源代碼如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace speedDemo
{
	public partial class MainForm : Form
	{
		private bool isContionsZi = false;
		private string findFile = string.Empty;
		
		
		public MainForm()
		{
			InitializeComponent();
			Control.CheckForIllegalCrossThreadCalls = false;
			this.CenterToScreen();
		}
		
		void Panel5SizeChanged(object sender, EventArgs e)
		{
			// 	20					10		/2=5
			panelMin.Left = (panelMax.Width - panelMin.Width) / 2;
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			
			isContionsZi = checkBox.Checked;
			findFile =  selectFile.Text;
			findFile = findFile.Trim();
            if(findFile==null || "".Equals(findFile))
            {
                 MessageBox.Show("路徑爲空");
                 return;
            }else if(!Directory.Exists(findFile))
			{
                 MessageBox.Show("無法訪問的路徑或不是目錄文件");
                 return;
            }
			
			button1.Enabled = false;
			masShow.Text = "";
			textBox.Text = "";			
			
			Thread t = new Thread(new ThreadStart(aa));
			t.Start();
		}
		
		public void aa()
		{
			//算出總數量,求出百分比
			string path  = findFile;
			DirectoryInfo info = new DirectoryInfo(path);
			
			FileSystemInfo[] fsif = info.GetFileSystemInfos();
			List<string> array = new List<string>();
			getSum(fsif,array);
			progressBar.Maximum = array.Count;
			
			//回顯到控制檯
			fildDir(array);
		}
		public void getSum(FileSystemInfo[] fsif,List<string> array)
		{
			foreach (FileSystemInfo fs in fsif)
			{
				array.Add(fs.FullName);
				
				if(isContionsZi)//是否包含子目錄
				{
		            DirectoryInfo direct = fs as DirectoryInfo;
					if(direct!=null)
					{
						getSum(direct.GetFileSystemInfos(),array);	
					}
				}
			}
		}
		
		public void fildDir(List<string> array)
		{
			Stopwatch watch = new Stopwatch();
			watch.Start();
			int sum = array.Count;
			//開始委託一個任務。
			((Action)delegate()
			 {
			 	System.Threading.Thread.Sleep(1000);
				for (int i = 1; i <=sum; i++) 
				{
				 	this.Invoke((Action)delegate()
				 	{
				 	    //控件沒有被釋放
				 	    if(!IsDisposed){
							textBox.AppendText(array[i-1]+"\r\n");
						}
				 	    
				 	});
					
			 		progressBar.Value = i;
					masShow.Text = ((double)i/sum).ToString("P");
					System.Threading.Thread.Sleep(500);
				}
				
			 	//執行結束後調用isRunTask方法收尾,傳遞多個參數
			 }).BeginInvoke(new AsyncCallback(isRunTask),new object[]{watch,array});
			
		}
		
		public void isRunTask(IAsyncResult ar)
		{
			//獲取委託傳遞的參數
			object[] para = ar.AsyncState as object[];
			if(para!=null && para.Length>=1)
			{
				Stopwatch watch = para[0] as Stopwatch;
				List<string> array = para[1] as List<string>;
				watch.Stop();
				TimeSpan ts = watch.Elapsed;
				MessageBox.Show("一共:"+array.Count+"文件,消耗 "+ts.Days+"天"+ts.Hours+"小時"+ts.Minutes+"分"+ts.Seconds+"秒"+ts.Milliseconds+"毫秒");
				button1.Enabled = true;
			}
		}
		
		void PanelDataMaxSizeChanged(object sender, EventArgs e)
		{
			panelDataMin.Left = (panelDataMax.Width-panelDataMin.Width)/2;
		}
		
		
		void PanelSpeedFuSizeChanged(object sender, EventArgs e)
		{
			//求出進度條的中間比例
			panelSpeedZi.Width = masShow.Width/2;
			panelSpeedZi.Left = (panelSpeedFu.Width-panelSpeedZi.Width)/2;
		}
	}
}
演示圖片:



發佈了56 篇原創文章 · 獲贊 16 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章