473,385 Members | 1,582 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.

Invoke?

How can I "invoke" a method containing parameters? I've tried
creating my own delegates and using them to do it, but can't get it
working. Can't find any info on this anywhere.

e.g.:

public class MainForm : Form
{

private delegate void D(char c);

private void Invoker()
{
D d = new D();
this.Invoke(d('t'));
}

private void Invokee(char c)
{
this.Text += c;
}

}
Nov 15 '05 #1
4 13065
The thing is that Invoke only takes a delegate with no parameters as a parameter so the parameters that you need will have to be passed in an indirect manner. You could use a class for wrapping up the variables

public class InvokeHelpe

private char c
private Form form

public InvokeHelper(char c,Form form

this.c = c
this.form = form
public Invokee(

form.Text += c

public class MainForm : For
private delegate void D()

private void Invoker(

InvokeHelpher ih = new InvokeHelper('t',this)
D d = new D(ih.Invokee)
this.Invoke(d)

----- C# Learner wrote: ----

How can I "invoke" a method containing parameters? I've trie
creating my own delegates and using them to do it, but can't get i
working. Can't find any info on this anywhere

e.g.

public class MainForm : For
private delegate void D(char c)

private void Invoker(

D d = new D()
this.Invoke(d('t'))
private void Invokee(char c

this.Text += c


Nov 15 '05 #2
Sijin Joseph <si*********@hotmail.com> wrote:
The thing is that Invoke only takes a delegate with no parameters as a parameter so the parameters that you need will have
to be passed in an indirect manner. You could use a class for wrapping up the variables.
Ah... the joys of object-oriented programming! :-)

Thanks.
public class InvokeHelper
{
private char c;
private Form form;

public InvokeHelper(char c,Form form)
{
this.c = c;
this.form = form;
}

public Invokee()
{
form.Text += c;
}
}

public class MainForm : Form
{

private delegate void D();

private void Invoker()
{
InvokeHelpher ih = new InvokeHelper('t',this);
D d = new D(ih.Invokee);
this.Invoke(d);
}

}


Nov 15 '05 #3
C# Learner <cs****@learner.here> wrote:
How can I "invoke" a method containing parameters?


Use the form of Invoke which takes two parameters - the first is the
delegate to invoke, the second is the parameter list (as an object
array). Here's a short but complete example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class MainForm : Form
{
delegate void D(char c);

MainForm()
{
SuspendLayout();

Size = new Size (200, 80);
Location = new Point (100, 100);

Button b = new Button();
b.Location = new Point (50, 20);
b.Size = new Size(100, 20);
b.Text = "Click me";
b.Click += new EventHandler (ButtonClicked);
Controls.Add(b);

ResumeLayout();
}

void ButtonClicked (object sender, EventArgs e)
{
D d = new D(Invokee);
Invoke (d, new object[]{'x'});
}

static void Main()
{
Application.Run (new MainForm());
}

void Invokee(char c)
{
this.Text += c;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
C# Learner <cs****@learner.here> wrote:
How can I "invoke" a method containing parameters?


Use the form of Invoke which takes two parameters - the first is the
delegate to invoke, the second is the parameter list (as an object
array). Here's a short but complete example:


<snip>

Thanks.
Nov 15 '05 #5

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

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
14
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
2
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.