473,654 Members | 3,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use DynamicInvoke method to return a value

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 !

Aug 2 '07 #1
0 1788

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

Similar topics

1
8160
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 is how I am creating a delegate object for "Subtract" method. BinaryOp op2 = (BinaryOp) Delegate.CreateDelegate(dt, tt,"Subtract"); Here is how I am creating parameters object. Notice t and ret are
18
4727
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
3
19281
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
1250
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 when triggerring an event or a delegate using DynamicInvoke() versus simply calling the event.. MyEvent.DynamicInvoke(sender, args);
1
3001
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 signature as the delegate. DynamicInvoke has a "fixed" signature, and it dynamically calls Invoke with the appropriate parameters." So far I have seen DynamicInvoke only used in conjunction with generics event arguments. Is there any other...
0
3859
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 string. write method should take a string. seek should take a number and return nuthing. use three member variables a buffer itself as a string, read_position, write_position. use >>, << methods. I have tried this code but i need full...
1
2701
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 dll: Private scihWnd As Integer 'Scintilla's hWnd Private SciFunc As Integer 'Scintilla's Main Function Call Private sciptr As Integer 'Scintilla's Direct Pointer Private scihmod As Integer 'Scintilla's hMod scihmod =...
5
9715
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> /// Invokes the wrapped delegate synchronously /// </summary> static void InvokeWrappedDelegate (Delegate d, object args)
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.