473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to use DynamicInvoke method to return a value

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_ProgressCha nged);
backWorker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(backWorker_RunWorke rCompleted);
}

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 !

Aug 2 '07 #1
0 1767

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Arif Khan | last post by:
How can I pass, by ref or out parameter values when calling DynamicInvoke Method. e.g. Here is my Delegate: public delegate int BinaryOp(int x, int y, out double z, ref bool overflow); Here...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Amir Shitrit | last post by:
Hi to all. Whats the difference between DynamicInvoke and Invoke methods of the Delegate class? Thanks in advance.
0
by: mickeymicks | last post by:
hi all! <i'm fairly new to csharp (and dotnet) and is particularly confused with delegates and events.. so forgive me if im a bit dense..> Is there any major concern that i need to think about...
1
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
Jon Skeet wrote in the .NET general newsgroup 11/17/2005: "The difference between Invoke and DynamicInvoke is that the parameters to Invoke depend on the delegate itself - the method has the same...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
1
by: Terry Olsen | last post by:
I'm using the SciLexer.dll to write a code editor for an in-house scripting language. I have the editor window up on a form but I need to use it's API. I've used the following commands to load the...
5
by: puzzlecracker | last post by:
I've just discovered DynamicInvoke methods while reading Jon's threading article. And my question is how it's different from a regular invoke method? In this method: /// <summary> ///...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.