1. 程式人生 > 實用技巧 >執行緒對話方塊基類

執行緒對話方塊基類

開啟對話方塊處理一些耗時任務時,需要實時顯示處理進度,每次設計執行緒、主執行緒訊息通訊和介面重新整理都比較煩,因此設計一個基類。

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

public class BaseForm : Form
    {
        public delegate void OutputMsgEventHandle(ThreadOutputMsg msg);
        
public event OutputMsgEventHandle OutputtedMsg; private SynchronizationContext _mainSynContext = null; private ThreadOutputMsg _msg = null; private void InitializeComponent() { this.SuspendLayout(); // // BaseForm // this
.ClientSize = new System.Drawing.Size(284, 261); this.Name = "BaseForm"; this.ResumeLayout(false); } public BaseForm() { this.Load += new EventHandler(BaseForm_Load); } public Thread StartThread(ParameterizedThreadStart start, object
parameter, OutputMsgEventHandle msgEnvent) { _mainSynContext = SynchronizationContext.Current; if (_msg == null) _msg = new ThreadOutputMsg(); //移除其他新增的事件 if (OutputtedMsg != null) { Delegate[] deles = OutputtedMsg.GetInvocationList(); foreach (Delegate dele in deles) { OutputtedMsg -= dele as OutputMsgEventHandle; } } if (msgEnvent != null) OutputtedMsg = msgEnvent; //開啟執行緒 Thread thread = new Thread(start); if (parameter == null) thread.Start(); else thread.Start(parameter); return thread; } /// <summary> /// 執行緒內部呼叫,傳送訊息到主執行緒,這樣就保證了OutputtedMsg事件肯定在主執行緒中執行 /// </summary> public virtual void OutputThreadMsg(int cur, int count, string msg, ThreadOutputMsgType msgType, object param = null) { if (OutputtedMsg == null || _mainSynContext == null) return; if (_msg == null) _msg = new ThreadOutputMsg(); _msg.Current = cur; _msg.Count = count; _msg.Message = msg; _msg.MsgType = msgType; _msg.Param = param; _mainSynContext.Send(new SendOrPostCallback(SendMessage_Callback), _msg); } /// <summary> /// 執行緒內部呼叫,傳送訊息到主執行緒,這樣就保證了OutputtedMsg事件肯定在主執行緒中執行 /// </summary> public virtual void OutputThreadMsg(ThreadOutputMsg msg) { if (OutputtedMsg == null || _mainSynContext == null) return; _mainSynContext.Send(new SendOrPostCallback(SendMessage_Callback), msg); } /// <summary> /// 主執行緒的同步物件SynchronizationContext.Send呼叫,這個是在主執行緒中執行的 /// </summary> public virtual void SendMessage_Callback(object obj) { if (OutputtedMsg != null) OutputtedMsg(obj as ThreadOutputMsg); } /// <summary> /// 獲取所有子檔案,輸出全路徑 /// </summary> /// <param name="prjPath">待分析目錄</param> /// <param name="filter">過濾字尾,預設".csproj",多個用英文';'隔開,字尾前的'.'不能省略</param> /// <param name="files">輸出的檔案全路徑列表</param> public void GetAllChildFiles(string destPath, ref List<string> files, string filter = null) { if (files == null) return; //獲取資料夾資訊 DirectoryInfo folder = new DirectoryInfo(destPath); if (!folder.Exists) return; //分析過濾字尾 bool bAll = false; if (string.IsNullOrWhiteSpace(filter) || filter == "*.*") bAll = true; else { filter = filter.Replace(" ", ""); filter = filter.Replace("*", ""); if (string.IsNullOrWhiteSpace(filter) || filter == ".") bAll = true; else { if (filter.Substring(filter.Length - 1) != ";") filter += ";"; filter = filter.ToLower(); } } //遍歷檔案 foreach (FileInfo file in folder.GetFiles()) { //不遍歷隱藏檔案 if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; if (bAll) files.Add(file.FullName); else if (string.IsNullOrWhiteSpace(file.Extension)) continue; else if (filter.IndexOf(file.Extension.ToLower() + ";") != -1 && file.FullName.IndexOf("DevExpress") == -1) files.Add(file.FullName); } //遍歷資料夾 foreach (DirectoryInfo child in folder.GetDirectories()) { //不遍歷隱藏資料夾 if ((child.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue; GetAllChildFiles(child.FullName, ref files, filter); } } public void GetAllChildFiles(string destPath, ref List<string> files, ref List<string> folders) { if (files == null || folders == null) return; //獲取資料夾資訊 DirectoryInfo folder = new DirectoryInfo(destPath); if (!folder.Exists) return; //遍歷檔案 foreach (FileInfo file in folder.GetFiles()) { files.Add(file.FullName); } //遍歷資料夾 foreach (DirectoryInfo child in folder.GetDirectories()) { folders.Add(child.FullName); GetAllChildFiles(child.FullName, ref files, ref folders); } } /// <summary> /// 完全退出程式 /// </summary> public void ExistApplication() { Process.GetCurrentProcess().Kill(); } private void BaseForm_Load(object sender, EventArgs e) { if (DesignMode) return; Button btnExit = new Button(); btnExit.Text = "退出"; btnExit.Location = new Point(this.Width - 16 + 1 - 37, -1);//可根據情況自由選擇位置或設定為變數 btnExit.Size = new Size(37, 20); btnExit.Click += new EventHandler(BtnExit_Click); this.Controls.Add(btnExit); btnExit.BringToFront(); btnExit.TabStop = false; } private void BtnExit_Click(object sender, EventArgs e) { ExistApplication(); } } public class ThreadOutputMsg { public int Current { get; set; } public int Count { get; set; } public string Message { get; set; } public ThreadOutputMsgType MsgType { get; set; } /// <summary> /// 額外引數,由使用者自定義 /// </summary> public object Param { get; set; } } public enum ThreadOutputMsgType { Error, Succeed, Normal}