using System;
using System.Windows. Forms;
using System.Threadin g;
using System.Componen tModel;
namespace ProgressControl .Core
{
public delegate object Execute(params object[] args);
public class ProgressControl ler
{
#region Construct
public ProgressControl ler(ProgressBar bar)
{
this.bar = bar;
InitComponet();
}
#endregion
#region Property
private Execute exec;
public Execute ExecuteHandle
{
set { exec = value; }
}
private ProgressBar bar;
/// <summary>
/// </summary>
public ProgressBar Bar
{
get { return bar; }
set { bar = value; }
}
private ProgressBarStyl e defaultStyle =
ProgressBarStyl e.Blocks;
/// <summary>
/// </summary>
public ProgressBarStyl e DefaultStype
{
get { return defaultStyle; }
set { defaultStyle = value; }
}
private object result = null;
public object Result
{
get { return result; }
}
private BackgroundWorke r backWorker = new BackgroundWorke r();
#endregion
#region Public
/// <summary>
///
/// </summary>
/// <returns></returns>
public void AsyncDo(params object[] args)
{
if (backWorker.IsB usy) throw new Exception("Anot her
process is processing.");
backWorker.RunW orkerAsync(args );
}
/// <summary>
///
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="step"></param>
/// <returns></returns>
public void AsyncDo(int min, int max, int step, params
object[] args)
{
//if (backWorker.IsB usy) throw new Exception("Anot her
process is processing.");
//int count = (int)((max - min) / step);
//do
//{
// if (backWorker.IsB usy) continue;
// if (count 0)
// {
// backWorker.RunW orkerAsync(args );
// ReportProgress( count);
// count--;
// }
// else
// break;
//}
//while (true);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public void MockAsyncDo(par ams object[] args)
{
if (backWorker.IsB usy) throw new Exception("Anot her
process is processing.");
/*
*
*/
bar.Style = ProgressBarStyl e.Marquee;
backWorker.RunW orkerAsync(args );
}
public void ReportProgress( int val)
{
if (val bar.Maximum)
{
val = bar.Maximum;
}
if (val < bar.Minimum)
{
val = bar.Minimum;
}
if (backWorker != null && backWorker.IsBu sy)
{
backWorker.Repo rtProgress(val) ;
}
}
public void ResetBar()
{
bar.Value = 0;
}
#endregion
#region Priavete
private void InitComponet()
{
backWorker.Work erReportsProgre ss = true;
backWorker.Work erSupportsCance llation = false;
backWorker.DoWo rk += new
DoWorkEventHand ler(backWorker_ DoWork);
backWorker.Prog ressChanged += new
ProgressChanged EventHandler(ba ckWorker_Progre ssChanged);
backWorker.RunW orkerCompleted += new
RunWorkerComple tedEventHandler (backWorker_Run WorkerCompleted );
}
void backWorker_RunW orkerCompleted( object sender,
RunWorkerComple tedEventArgs e)
{
if (e.Error != null)
{
throw e.Error;
}
bar.Value = bar.Maximum;
bar.Style = defaultStyle;
result = e.Result;
}
void backWorker_DoWo rk(object sender, DoWorkEventArgs e)
{
if (exec == null) throw new Exception("The property of
'ExecuteHandle' doesnot set method name,please check it first.");
try
{
e.Result = exec.DynamicInv oke(e.Argument) ;
}
catch (Exception ex)
{
throw ex;
}
}
void backWorker_Prog ressChanged(obj ect sender,
ProgressChanged EventArgs e)
{
bar.Value = e.ProgressPerce ntage;
}
#endregion
}
}
I write the class ,and want to use the class to update the
progressbar .But Now one problem occurred.,I cannot get the return
value:
for example:
ProgressControl ler c = new
ProgressControl ler(this.progre ssBar5);
c.ExecuteHandle = new Execute(SimpleC omputeFibonacci );
c.AsyncDo(50);
MessageBox.Show (c.Result.ToStr ing()); //null exception
occurred!
and think it because the thread and the delegate but i can not solute
it , Help me !
Thanks a lot !