// // Copyright © 2006-2010, Intel Corporation. All rights reserved. // // File: ProgressWindow.cs // // Contents: Defintion of logic of the form used for progress window used by various parts of the code // using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace AMT_SW_GUI { public partial class ProgressWindow : 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; private void btnCancel_Click(object sender, EventArgs e) { this.backgroundWorker1.CancelAsync(); this.Close(); } #endregion PRIVATE //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; 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(); } #region PUBLIC public ProgressWindow(Delegate funcToRun, object[] inpParams, string formTitle, string text, bool hasCancel, bool to100) { InitializeComponent(); InitUI(); this.Text = formTitle; if (!hasCancel) { btnCancel.Visible = false; Size tempSize = this.Size; tempSize.Height = tempSize.Height - btnCancel.Size.Height; this.Size = tempSize; } if (!to100) { this.prCompleted.Style = ProgressBarStyle.Marquee; this.prCompleted.MarqueeAnimationSpeed = 100; } if (text != "") lblHeaderDesc.Text = text; int diff = Math.Max(0, lblHeaderDesc.Size.Height - 15); innerPane1.Size = new Size(innerPane1.Size.Width, innerPane1.Size.Height + diff); prCompleted.Location = new Point(prCompleted.Location.X, prCompleted.Location.Y + diff); btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + diff); this.Size = new Size(this.Size.Width, this.Size.Height + diff); 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; } } } }