123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace LYFZ.ComponentLibrary
- {
- /// <summary>
- /// 进度状态显示类型
- /// </summary>
- public enum LoadHandlingType {
- /// <summary>
- /// 加载固定状态显示,只显示进度动画,不显示具体进度值
- /// </summary>
- LoadProgress,
- /// <summary>
- /// 执行进度条显示,显示具体进度值
- /// </summary>
- ExecutionProgress,
- }
- public delegate void ExecutionBackgroundDoWork(object argument, BackgroundWorker backgroundWorker);
- public delegate void LoadBackgroundDoWork(object argument, BackgroundWorker backgroundWorker);
- /// <summary>
- /// 程序在执行时显示的进度状态窗体
- /// </summary>
- public partial class FrmLoadHandling : Form
- {
- /// <summary>
- ///
- /// </summary>
- private BackgroundWorker backgroundWorker = new BackgroundWorker();
- /// <summary>
- /// 独立执行线程
- /// </summary>
- public BackgroundWorker BackgroundWorker
- {
- get { return backgroundWorker; }
- set { backgroundWorker = value; }
- }
- /// <summary>
- ///
- /// </summary>
- private ExecutionBackgroundDoWork executionBackgroundDoWorkMethod = null;
- /// <summary>
- /// ExecutionProgress要执行的方法委托,设置要执行的法
- /// </summary>
- public ExecutionBackgroundDoWork ExecutionBackgroundDoWorkMethod
- {
- get { return executionBackgroundDoWorkMethod; }
- set { executionBackgroundDoWorkMethod = value; }
- }
- /// <summary>
- ///
- /// </summary>
- private LoadBackgroundDoWork loadBackgroundDoWorkMethod = null;
- /// <summary>
- /// LoadProgress要执行的方法委托,设置要执行的法
- /// </summary>
- public LoadBackgroundDoWork LoadBackgroundDoWorkMethod
- {
- get { return loadBackgroundDoWorkMethod; }
- set { loadBackgroundDoWorkMethod = value; }
- }
- LoadProgress loadProgress = new LoadProgress();
- /// <summary>
- /// LoadProgress固定滚动进度条
- /// </summary>
- public LoadProgress LoadProgressControl
- {
- get { return loadProgress; }
- set { loadProgress = value; }
- }
- ExecutionProgress executionProgress = new ExecutionProgress();
- /// <summary>
- /// ExecutionProgress进度值滚动进度条
- /// </summary>
- public ExecutionProgress ExecutionProgressControl
- {
- get { return executionProgress; }
- set { executionProgress = value; }
- }
- ManualResetEvent eventDone = new ManualResetEvent(false);
- /// <summary>
- ///
- /// </summary>
- public ManualResetEvent EventDone
- {
- get { return eventDone; }
- set { eventDone = value; }
- }
-
- public FrmLoadHandling(bool isInitializeBackgroundWorker)
- {
- this.isInitializeBackgroundWorker = isInitializeBackgroundWorker;
- InitializeBackgroundWorker(LoadHandlingType.LoadProgress);
- }
- public FrmLoadHandling()
- {
- InitializeBackgroundWorker(LoadHandlingType.LoadProgress);
- }
- LoadHandlingType _type = LoadHandlingType.LoadProgress;
- public FrmLoadHandling(LoadHandlingType type)
- {
- InitializeBackgroundWorker(type);
- }
- bool isInitializeBackgroundWorker = true;
- /// <summary>
- /// 初始化独立线程
- /// </summary>
- public void InitializeBackgroundWorker(LoadHandlingType type)
- {
- try
- {
- InitializeComponent();
-
- this._type = type;
- switch (this._type)
- {
- case LoadHandlingType.LoadProgress:
- loadProgress.Location = new Point(0, 0);
- loadProgress.Dock = DockStyle.Fill;
- loadProgress.Name = "load" + DateTime.Now.ToString("yyyyMMddhhmmssff");
- this.Controls.Add(loadProgress);
- break;
- case LoadHandlingType.ExecutionProgress:
- this.Width = 280;
- executionProgress.Location = new Point(0, 0);
- executionProgress.Dock = DockStyle.Fill;
- executionProgress.Name = "exe" + DateTime.Now.ToString("yyyyMMddhhmmssff");
- this.Controls.Add(executionProgress);
- break;
- }
- if (isInitializeBackgroundWorker)
- {
- this.Load += new System.EventHandler(this.FrmLoadHandling_Load);
- backgroundWorker.WorkerReportsProgress = true;
- backgroundWorker.WorkerSupportsCancellation = true;
- backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
- backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
- backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
- }
-
- }
- catch { }
- }
- void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- try
- {
- if (e.Cancelled)
- {
- this.EventDone.Set();
- this.DialogResult = DialogResult.Cancel;
-
- // MessageBox.Show("操作取消");
- }
- else
- {
- this.EventDone.Set();
- this.DialogResult = DialogResult.OK;
- // MessageBox.Show("操作完成");
- }
- }
- catch {
- this.EventDone.Set();
- this.DialogResult = DialogResult.Cancel;
- }
- }
- void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- try
- {
- if (e.ProgressPercentage == -1)
- {
- this.Opacity = 0;
- return;
- }
- else
- {
- this.Opacity = 100;
- }
- if (this._type == LoadHandlingType.ExecutionProgress)
- {
- if (e.ProgressPercentage >= executionProgress.MySkinProgressBar.Maximum)
- {
- executionProgress.MySkinProgressBarValue = executionProgress.MySkinProgressBar.Maximum;
- }
- else
- {
- executionProgress.MySkinProgressBarValue = e.ProgressPercentage;
- }
- if (e.UserState != null)
- {
- ExecutionProgressControl.LbLoadProgress.Text = e.UserState.ToString();
- }
- }
- if (this._type == LoadHandlingType.LoadProgress)
- {
- if (e.UserState != null)
- {
- LoadProgressControl.LbLoadProgress.Text = e.UserState.ToString();
- }
- }
- }
- catch { }
- }
- void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- //try
- //{
- Form frmP = this.ParentForm;
- switch (this._type)
- {
- case LoadHandlingType.ExecutionProgress:
- if (ExecutionBackgroundDoWorkMethod != null)
- {
- ExecutionBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
- e.Result = true;
- }
- else
- {
- e.Result = false;
- }
- break;
- case LoadHandlingType.LoadProgress:
- if (LoadBackgroundDoWorkMethod != null)
- {
- LoadBackgroundDoWorkMethod(e.Argument, this.BackgroundWorker);
- e.Result = true;
- }
- else
- {
- e.Result = false;
- }
- break;
- }
- //}
- // catch (Exception ex){
- // MessageBoxCustom.Show(ex.Message);
- //}
-
- }
- /// <summary>
- /// 执行指定方法并显示固定状态进度条
- /// </summary>
- /// <param name="doWorkMethod">要执行指定的方法</param>
- /// <param name="argument">参数</param>
- /// <returns></returns>
- public static DialogResult LoadDoWorkMethod(LoadBackgroundDoWork doWorkMethod, object argument = null)
- {
- try
- {
- FrmLoadHandling frm = new FrmLoadHandling();
- // frm.TopMost = true;
- frm.LoadBackgroundDoWorkMethod = doWorkMethod;
- frm.BackgroundWorker.RunWorkerAsync(argument);
- return frm.ShowDialog();
- }
- catch {
- return DialogResult.Cancel;
- }
- }
- /// <summary>
- /// 执行指定方法并显示具体进度值状态进度条
- /// </summary>
- /// <param name="doWorkMethod">要执行指定的方法</param>
- /// <param name="argument">参数</param>
- /// <param name="width">显示进度条窗体的宽度,值为:190~800 之间的整数</param>
- /// <returns></returns>
- public static DialogResult ExecutionDoWorkMethod(ExecutionBackgroundDoWork doWorkMethod, object argument = null, int width = 190)
- {
- try
- {
- FrmLoadHandling frm = new FrmLoadHandling(LoadHandlingType.ExecutionProgress);
- // frm.TopMost = true;
- if (width > 190 && width < 800)
- {
- frm.Width = width;
- }
- frm.ExecutionBackgroundDoWorkMethod = doWorkMethod;
- frm.BackgroundWorker.RunWorkerAsync(argument);
- return frm.ShowDialog();
- }
- catch {
- return DialogResult.Cancel;
- }
- }
- private void FrmLoadHandling_Load(object sender, EventArgs e)
- {
- // backgroundWorker.RunWorkerAsync();
- }
- }
- }
|