using System; using System.Diagnostics; namespace ProcessStatus { public class Progress { public event System.EventHandler Item1CountCalculated; public event System.EventHandler Item2CountCalculated; public event System.EventHandler Item1Processing; public event System.EventHandler Item2Processing; public event System.EventHandler Load; public event System.EventHandler UnLoad; public string CompletionMessage = ""; public string Item1Description = ""; public int Item1Count = 0; public int Item1ProcessedCount = 0; public string Item2Description = ""; public int Item2Count = 0; public int Item2ProcessedCount = 0; public void OnItem2Processing(string description) { if ( Item2Processing == null ) { return; } this.Item2Description = description; this.Item2ProcessedCount++; Item2Processing.BeginInvoke( this, new EventArgs(), new AsyncCallback(Item2ProcessingCompleted),null); } private void Item2ProcessingCompleted( IAsyncResult ar ) { if ( Item2Processing == null ) { return; } Item2Processing.EndInvoke( ar ); } public void OnItem1Processing(string description) { if ( Item1Processing == null ) { return; } this.Item1Description = description; this.Item1ProcessedCount++; Item1Processing.BeginInvoke( this, new EventArgs(), new AsyncCallback(Item1ProcessingCompleted),null); } private void Item1ProcessingCompleted( IAsyncResult ar ) { if ( Item1Processing == null ) { return; } Item1Processing.EndInvoke( ar ); } public void OnItem1CountCalculated(int count) { this.Item1Count = count; this.Item1ProcessedCount = 0; Item1CountCalculated.BeginInvoke( this, new EventArgs(), new AsyncCallback(Item1CountCalculatedCompleted),null); } private void Item1CountCalculatedCompleted( IAsyncResult ar ) { if ( Item1CountCalculated == null ) { return; } Item1CountCalculated.EndInvoke( ar ); } public void OnItem2CountCalculated(int count) { this.Item2Count = count; this.Item2ProcessedCount = 0; Item2CountCalculated.BeginInvoke( this, new EventArgs(), new AsyncCallback(Item2CountCalculatedCompleted),null); } private void Item2CountCalculatedCompleted( IAsyncResult ar ) { if ( Item2CountCalculated == null ) { return; } Item2CountCalculated.EndInvoke( ar ); } public void OnLoad(string item1Description,string item2Description) { if ( Load == null ) { return; } this.Item1Description = item1Description; this.Item1Count = 0; this.Item1ProcessedCount = 0; this.Item2Description = item2Description; this.Item2Count = 0; this.Item2ProcessedCount = 0; this.Load(this, new EventArgs()); Load.BeginInvoke( this, new EventArgs(), new AsyncCallback(LoadCompleted),null); } private void LoadCompleted( IAsyncResult ar ) { if ( Load == null ) { return; } Load.EndInvoke( ar ); } public void OnUnLoad(string completionMessage) { this.CompletionMessage = completionMessage; UnLoad.BeginInvoke( this, new EventArgs(), new AsyncCallback(UnLoadCompleted),null); } private void UnLoadCompleted( IAsyncResult ar ) { if ( UnLoad == null ) { return; } UnLoad.EndInvoke( ar ); } } }
using System; using System.Data; using System.Data.OleDb; using System.Threading; namespace BusinessLogic { public class Sample : ProcessStatus.Progress { public Sample() { } public bool Method1(string connectionString,string someParameter) { this.OnLoad("First Step In Process Beginning",""); Method2(connectionString); this.OnUnLoad("All done"); return true; } private void Method2(string connectionString) { int maxtrys = 10; this.OnItem1CountCalculated(maxtrys); for(int i=0;i<maxtrys;i++) { this.OnItem1Processing("main process " + i.ToString() + " of " + maxtrys.ToString()); Method3(connectionString); } } private void Method3(string connectionString) { DataTable dt = new DataTable(); int maxtrys=20; string sql = "select * from Hierarchy"; try { this.OnItem2CountCalculated(maxtrys); for(int i=0;i<maxtrys;i++) { this.OnItem2Processing("sub process " + i.ToString() + " of " + maxtrys.ToString()); using(OleDbDataAdapter da = new OleDbDataAdapter(sql,connectionString)) { da.Fill(dt); } } } catch (Exception) { throw; } } } }
using System; namespace Sample { public class LongRunningMethods : ReusableForms.ProgressWatcher { public void MyProcessThatTakesAwhile(string connectionString,string someKeyParameter) { try { string desc = ""; desc += "This is a really long description explaining some stuff "; desc += "regarding what in the world is transpiring while these neat "; desc += "progress bars move around."; this.DefineSettings(false,desc,"Preparing To Run...",""); this.Show(); BusinessLogic.Sample logic = new BusinessLogic.Sample(); logic.Load += new System.EventHandler(this.Progress_OnLoad); logic.UnLoad += new System.EventHandler(this.Progress_OnUnLoad); logic.Item1CountCalculated += new System.EventHandler(this.Progress_OnItem1Start); logic.Item1Processing += new System.EventHandler(this.Progress_OnItem1Change); logic.Item2Processing += new System.EventHandler(this.Progress_OnItem2Change); logic.Item2CountCalculated += new System.EventHandler(this.Progress_OnItem2Start); Hourglass(true); logic.Method1(connectionString,someKeyParameter); } catch (Exception err) { ShowError(err.Message); } finally { Hourglass(false); } } } }
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Diagnostics; using System.IO; using System.Threading; namespace ReusableForms { public class ProgressWatcher : System.Windows.Forms.Form { protected bool CloseOnCompletion = false; private System.Windows.Forms.Label lblItem1Description; private System.Windows.Forms.Label lblItem2Description; private System.Windows.Forms.Label lblCompletionMessage; private System.Windows.Forms.Label lblTopBar; private System.Windows.Forms.Label lblDescription; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.ProgressBar progressBar2; private System.Windows.Forms.Button cmdClose; private System.ComponentModel.Container components = null; public ProgressWatcher() { InitializeComponent(); } protected void DefineSettings(bool closeOnCompletion, string summaryDescription, string item1Description, string item2Description) { InitFormElements(closeOnCompletion,summaryDescription); this.lblItem1Description.Text = item1Description; this.lblItem2Description.Text = item2Description; } protected void DefineSettings(bool closeOnCompletion, string summaryDescription, string item1Description) { InitFormElements(closeOnCompletion,summaryDescription); this.lblItem1Description.Text = item1Description; this.lblItem2Description.Visible = false; this.progressBar2.Visible = false; } private void InitFormElements(bool closeOnCompletion, string summaryDescription) { this.lblCompletionMessage.Text = ""; this.lblDescription.Text = summaryDescription; this.CloseOnCompletion = closeOnCompletion; } protected void Progress_OnItem1Start(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; this.progressBar1.Value = 0; this.progressBar1.Maximum = prg.Item1Count; Application.DoEvents(); } catch { } } protected void Progress_OnItem2Start(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; this.progressBar2.Value = 0; this.progressBar2.Maximum = prg.Item2Count; Application.DoEvents(); } catch { } } protected void Progress_OnItem1Change(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; this.lblItem1Description.Text = prg.Item1Description; this.progressBar1.Value = prg.Item1ProcessedCount; this.lblItem1Description.Refresh(); Application.DoEvents(); } catch { } } protected void Progress_OnItem2Change(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; this.lblItem2Description.Text = prg.Item2Description; this.progressBar2.Value = prg.Item2ProcessedCount; this.lblItem2Description.Refresh(); Application.DoEvents(); } catch { } } protected void Progress_OnLoad(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; if (prg.Item1Description.Length > 0) { this.lblItem1Description.Text = prg.Item1Description; } if (prg.Item2Description.Length > 0) { this.lblItem2Description.Text = prg.Item2Description; } Application.DoEvents(); } catch { } } protected void Progress_OnUnLoad(object sender, System.EventArgs e) { try { ProcessStatus.Progress prg = (ProcessStatus.Progress)sender; this.lblCompletionMessage.Text = prg.CompletionMessage; this.cmdClose.Visible = true; Application.DoEvents(); } catch { } } protected void ShowError(string msg) { Hourglass(false); MessageBox.Show(msg); } protected void Hourglass(bool Show) { if (Show == true) { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; } else { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } return; } private void cmdClose_Click(object sender, System.EventArgs e) { this.Close(); } } }