//---------------------------------------------------------------------------- // // Copyright © 2009-2014, Intel Corporation. All rights reserved. // // File: ProgressWindow.cs // // Contents: Definition of logic of the form used for progress window used by various parts of the code // //---------------------------------------------------------------------------- using System; using UCT.Forms; using AMT_SW_GUI; using System.Drawing; using System.Windows.Forms; using System.ComponentModel; namespace UCT.Forms { /// /// Progress bar window /// public partial class ProgressBarWindow : Form { #region - Private - private string m_uiObjectName = "ProgressWindow"; private string m_helppath; private Exception _exceptionCaught = null; Delegate exeFunc; private System.ComponentModel.BackgroundWorker backgroundWorker1 = new BackgroundWorker(); BackgroundWorker m_Worker; public Object[] m_return_obj; private object[] passToFunc; #endregion //If an exception occured exposing exception to the user public Exception ExceptionCaught { get { return _exceptionCaught; } } private void InitializeBackgoundWorker() { backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // Get the BackgroundWorker that raised this event. m_Worker = sender as BackgroundWorker; if (m_Worker != null) { m_Worker.WorkerReportsProgress = true; m_Worker.WorkerSupportsCancellation = true; m_return_obj = (object[])exeFunc.DynamicInvoke(m_Worker, e, passToFunc); e.Result = 1; } } // This event handler deals with the results of the // background operation. private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { if (e.Error.InnerException != null) { _exceptionCaught = e.Error.InnerException; } else { _exceptionCaught = e.Error; } } this.btnCancel.Enabled = false; this.Close(); } // This event handler updates the progress bar. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (e.UserState is BackgroundInvocation) { BackgroundInvocation invocationState = (BackgroundInvocation)e.UserState; if (invocationState.UserState.ToString().CompareTo("") != 0) this.lblOptional.Text = (string)invocationState.UserState; if (null != invocationState.FuncToRun) { if(null == m_Worker) m_Worker = sender as BackgroundWorker; invocationState.FuncToRun.DynamicInvoke(invocationState.Params); } } this.prCompleted.Value = Math.Min(e.ProgressPercentage, 100); this.Update(); } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Ignore; this.backgroundWorker1.CancelAsync(); this.Close(); } private void btnSkip_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Abort; this.backgroundWorker1.CancelAsync(); this.Close(); } #region - Public - public ProgressBarWindow() { InitializeComponent(); InitializeBackgoundWorker(); } public ProgressBarWindow(Delegate funcToRun, string formTitle, object[] inpParams, string text, bool hasCancel, bool hasSkip, bool to100) { InitializeComponent(); backgroundWorker1.WorkerSupportsCancellation = true; InitUI(); this.Text = formTitle; if (!hasCancel && !hasSkip) { btnCancel.Visible = false; Size tempSize = this.Size; tempSize.Height = tempSize.Height - btnCancel.Size.Height; this.Size = tempSize; } else if (hasSkip == !hasCancel) { if (hasSkip) { btnCancel.Visible = false; } else { btnCancel.Location = new Point((this.Width - btnCancel.Width) / 2, btnCancel.Location.Y); } } else { btnCancel.Visible = true; } if (!to100) { this.prCompleted.Style = ProgressBarStyle.Marquee; this.prCompleted.MarqueeAnimationSpeed = 100; } if (text != "") lblHeaderDesc.Text = text; exeFunc = funcToRun; passToFunc = inpParams; InitializeBackgoundWorker(); } public void InitUI() { } public void LoadResources() { } public void SetPrevileges() { } public void FetchData() { } public void Help() { } #endregion PUBLIC #region - Properties - public string HelpPath { get { return m_helppath; } set { m_helppath = value; } } public string UIObjectName { get { return m_uiObjectName; } set { m_uiObjectName = value; } } #endregion PROPERTIES } class BackgroundInvocation { private string m_UserState = string.Empty; private Delegate m_FuncToRun; private object[] m_Params; public BackgroundInvocation(string state, Delegate funcToRun, object[] inParams) { m_UserState = state; m_FuncToRun = funcToRun; m_Params = inParams; } public string UserState { get { return m_UserState; } set { m_UserState = value; } } public Delegate FuncToRun { get { return m_FuncToRun; } set { m_FuncToRun = value; } } public object[] Params { get { return m_Params; } set { m_Params = value; } } } }