how to use DynamicInvoke method to return a value

Discussion in 'ASP .Net' started by flyingchen, Aug 2, 2007.

  1. flyingchen

    flyingchen Guest

    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 !
     
    flyingchen, Aug 2, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,990
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    504
    Richard Bos
    Jun 18, 2004
  3. Carl
    Replies:
    21
    Views:
    1,009
    Patricia Shanahan
    Aug 24, 2006
  4. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  5. Xeno Campanoli
    Replies:
    0
    Views:
    238
    Xeno Campanoli
    Feb 13, 2006
Loading...

Share This Page