public partial class ProcessBox : Form
{
public delegate void SetPos(int ipos);
public delegate void Complete();
public ProcessBox(string formText,string text, int maxValue)
{
InitializeComponent();
this.l_Waiting.Text = text;
this.Text = formText;
pb_processBox.Maximum = maxValue;
}
public void SetProcessBarValue(int value)
{
if (value != this.pb_processBox.Value)
{
SetPos setpos = new SetPos(UpdataProcessbar);
setpos(value);
}
}
public void Completed()
{
Complete complete = new Complete(DoComplete);
complete();
}
private void DoComplete()
{
if (this.InvokeRequired)
{
Complete complete = new Complete(DoComplete);
this.Invoke(complete);
}
else
{
this.Close();
}
}
private void UpdataProcessbar(int ipos)
{
if (this.InvokeRequired)
{
SetPos setpos = new SetPos(UpdataProcessbar);
this.Invoke(setpos, new object[] { ipos });
}
else
{
this.pb_processBox.Value = ipos;
}
}
}
public class ProcessBoxManager
{
public delegate void ProcessChanged(int process);
public event ProcessChanged OnProcessChanged;
public delegate void Completed();
public event Completed OnCompleted;
class StepInfor
{
public int stepCount;
public int currStep;
}
private ProcessBox _processBox = null;
private Stack<StepInfor> _stepInforStack = new Stack<StepInfor>();
//private StepInfor _stepInfor;
private int _process = 0;//0-100
public void PushStep(int stepCount)
{
if (stepCount <= 0)
{
stepCount = 1;
}
StepInfor _stepInfor;
_stepInfor = new StepInfor();
_stepInfor.stepCount = stepCount;
_stepInfor.currStep = 0;
_stepInforStack.Push(_stepInfor);
UpdataProcess();
}
public void NextStep()
{
StepInfor _stepInfor;
_stepInfor = _stepInforStack.Peek();
if (_stepInfor.currStep < _stepInfor.stepCount)
{
_stepInfor.currStep += 1;
}
UpdataProcess();
}
public void PopStep()
{
_stepInforStack.Pop();
}
private void UpdataProcess()
{
int process = 0;
int steps = 1;
StepInfor[] stepinfors = _stepInforStack.ToArray();
for (int i = stepinfors.Length - 1; i >= 0; i--)
{
StepInfor step = stepinfors[i];
steps = steps * step.stepCount;
process += (step.currStep) * 100 / steps;
}
if( _process != process)
{
_process = process;
OnProcessChanged(_process);
//_processBox.SetProcessBarValue(process);
}
}
public void ShowProcessBox(string formText,string text)
{
_process = 0;
_processBox = new ProcessBox(formText,text, 100);
_processBox.TopMost = true;
OnProcessChanged += new ProcessChanged(_processBox.SetProcessBarValue);
OnCompleted += new Completed(_processBox.Completed);
new Thread((ThreadStart)delegate
{
Application.Run(_processBox);
}).Start();
}
public void CloseProcessBox()
{
OnCompleted();
}
}