how to use DynamicInvoke method to return a value

F

flyingchen

using System;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;

namespace ProgressControl.Core
{
public delegate object Execute(params object[] args);

public class ProgressController
{
#region Construct
public ProgressController(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 ProgressBarStyle defaultStyle =
ProgressBarStyle.Blocks;
/// <summary>
/// </summary>
public ProgressBarStyle DefaultStype
{
get { return defaultStyle; }
set { defaultStyle = value; }
}

private object result = null;

public object Result
{
get { return result; }
}

private BackgroundWorker backWorker = new BackgroundWorker();

#endregion

#region Public

/// <summary>
///
/// </summary>
/// <returns></returns>
public void AsyncDo(params object[] args)
{
if (backWorker.IsBusy) throw new Exception("Another
process is processing.");

backWorker.RunWorkerAsync(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.IsBusy) throw new Exception("Another
process is processing.");

//int count = (int)((max - min) / step);

//do
//{
// if (backWorker.IsBusy) continue;
// if (count > 0)
// {
// backWorker.RunWorkerAsync(args);
// ReportProgress(count);
// count--;
// }
// else
// break;
//}
//while (true);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public void MockAsyncDo(params object[] args)
{
if (backWorker.IsBusy) throw new Exception("Another
process is processing.");

/*
*
*/
bar.Style = ProgressBarStyle.Marquee;
backWorker.RunWorkerAsync(args);
}

public void ReportProgress(int val)
{
if (val > bar.Maximum)
{
val = bar.Maximum;
}
if (val < bar.Minimum)
{
val = bar.Minimum;
}

if (backWorker != null && backWorker.IsBusy)
{
backWorker.ReportProgress(val);
}
}

public void ResetBar()
{
bar.Value = 0;
}
#endregion

#region Priavete

private void InitComponet()
{
backWorker.WorkerReportsProgress = true;
backWorker.WorkerSupportsCancellation = false;

backWorker.DoWork += new
DoWorkEventHandler(backWorker_DoWork);
backWorker.ProgressChanged += new
ProgressChangedEventHandler(backWorker_ProgressChanged);
backWorker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(backWorker_RunWorkerCompleted);
}

void backWorker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
throw e.Error;
}

bar.Value = bar.Maximum;
bar.Style = defaultStyle;

result = e.Result;
}

void backWorker_DoWork(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.DynamicInvoke(e.Argument);
}
catch (Exception ex)
{
throw ex;
}
}

void backWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
bar.Value = e.ProgressPercentage;
}

#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:
ProgressController c = new
ProgressController(this.progressBar5);
c.ExecuteHandle = new Execute(SimpleComputeFibonacci);
c.AsyncDo(50);
MessageBox.Show(c.Result.ToString()); //null exception
occurred!
and think it because the thread and the delegate but i can not solute
it , Help me !
Thanks a lot !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top