473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control.Invoke and ref parameter

Hi,

Does anyone know how Control.Invoke can invoke a method which has ref
parameters?

For example,

class A {
public delegate MyFuncHandler(r ef int i, ref int j);

public void MyFunc(ref int i, ref int j)
{
..
}

public A

public void MyCaller(DataGr idView oDataGridView)
{
int x = 1;
int y = 1;

...

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { ref x, ref y });

....
}

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { ref x, ref y });

produces compilation error. Am I doing this wrong, or Control.Invoke does
not allow method with ref parameters?

Thanks,

--
George
Jun 23 '06 #1
7 13110
George <wa**@nospam.no spam> wrote:
Does anyone know how Control.Invoke can invoke a method which has ref
parameters? oDataGridView.I nvoke(new MyHandler(MyFun c), new object { ref x, ref y });


instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);
// use args[0] (returned value of x)
// use args[1] (returned value of y)

Now, args will have been modified based on what MyFunc did to x and y.

-- Barry

--
http://barrkel.blogspot.com/
Jun 23 '06 #2
Hi,

Tried it, but I don't think it worked.
instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);
This would have been equivalent to

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { x, y});

Above, I believe, pass x and y by value, instead of by reference.

--
George
"Barry Kelly" wrote:
George <wa**@nospam.no spam> wrote:
Does anyone know how Control.Invoke can invoke a method which has ref
parameters?

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { ref x, ref y });


instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);
// use args[0] (returned value of x)
// use args[1] (returned value of y)

Now, args will have been modified based on what MyFunc did to x and y.

-- Barry

--
http://barrkel.blogspot.com/

Jun 23 '06 #3
George <wa**@nospam.no spam> wrote:
Tried it, but I don't think it worked.
instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);


This would have been equivalent to

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { x, y});

Above, I believe, pass x and y by value, instead of by reference.


Did you even try the code? The arguments array passed in receives the
changed values.

To repeat myself, x and y don't change, the values in the *array*
change. The code above *is* *not* *equivalent*, because you've thrown
away the reference to the array.

When I said "// use args[0]", I meant that it receives the new value of
x.

For example, try this code (in C# 2.0, but the principle is the same):

---8<---
using System;
using System.Windows. Forms;
using System.Threadin g;

class App
{
delegate void IntFiddler(ref int value);

static void Main()
{
Form form = new Form();
ThreadPool.Queu eUserWorkItem(d elegate
{
Thread.Sleep(10 00);

// Run some code in the context of the UI.
object[] args = { 21 };
form.Invoke(new IntFiddler(Fidd le), args);
Console.WriteLi ne(args[0]);
});
Application.Run (form);
}

static void Fiddle(ref int x)
{
x *= 2;
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Jun 23 '06 #4
George wrote:
Hi,

Tried it, but I don't think it worked.
instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);
This would have been equivalent to

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { x, y});

Above, I believe, pass x and y by value, instead of by reference.



Hi George,
This would have been equivalent to


I'm afraid it's not equivalent. Quite different in fact. In your code, you
immediately instantiate an object array straight into the Invoke routine.
In Barry's code, the array is instantiated *outside* of the routine. It's
the instance of the array that changes, not the instance of the values
inside the array.

-- Tom Spink
Jun 23 '06 #5
Hi Barry.

Thanks for your help again. It is my mistake that I checked against the
wrong variables.

In your first example, I checked against the value in x and y, instead of
the new values in args.

If you don't mind telling me: I spent a lot of time searching for an answer
to this question before I posted here. I must have been searching the wrong
topic. Do you know if Microsoft published a document regarding the proper
use of

Control.Invoke( Delegate, object[])

especially when the Delegate contains "ref" or "out" parameters. It seems
like

1. without "ref" or "out", the object array *contents* are not modified.

2. with "ref" or "out", the object array * contents" are modified.

Is this a method marshalling thing?

Thanks

--
George
"Barry Kelly" wrote:
George <wa**@nospam.no spam> wrote:
Tried it, but I don't think it worked.
instead use:

object[] args = { x, y };
oDataGridView.I nvoke(new MyHandler(MyFun c), args);


This would have been equivalent to

oDataGridView.I nvoke(new MyHandler(MyFun c), new object { x, y});

Above, I believe, pass x and y by value, instead of by reference.


Did you even try the code? The arguments array passed in receives the
changed values.

To repeat myself, x and y don't change, the values in the *array*
change. The code above *is* *not* *equivalent*, because you've thrown
away the reference to the array.

When I said "// use args[0]", I meant that it receives the new value of
x.

For example, try this code (in C# 2.0, but the principle is the same):

---8<---
using System;
using System.Windows. Forms;
using System.Threadin g;

class App
{
delegate void IntFiddler(ref int value);

static void Main()
{
Form form = new Form();
ThreadPool.Queu eUserWorkItem(d elegate
{
Thread.Sleep(10 00);

// Run some code in the context of the UI.
object[] args = { 21 };
form.Invoke(new IntFiddler(Fidd le), args);
Console.WriteLi ne(args[0]);
});
Application.Run (form);
}

static void Fiddle(ref int x)
{
x *= 2;
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/

Jun 26 '06 #6
George <wa**@nospam.no spam> wrote:
If you don't mind telling me: I spent a lot of time searching for an answer
to this question before I posted here. I must have been searching the wrong
topic. Do you know if Microsoft published a document regarding the proper
use of

Control.Invoke( Delegate, object[])
I don't know. I originally (several years ago) just guessed that's what
it would do when looking at MethodBase.Invo ke(), and ran a program to
test out the theory. I was right :)
Is this a method marshalling thing?


In so far as the CLR needs to take the arguments out of the array and
put them on the stack and in regesters to call the method, and then copy
out all the modified 'ref' and 'out' parameters back into the array,
yes.

-- Barry

--
http://barrkel.blogspot.com/
Jun 26 '06 #7
Hi George,

In addition to Barry's reply, I still want to add some comment to your 2
questions:

Based on the C# language reference, "ref" is a method parameter keyword,
which is only used in the parameter decoration.
1. without "ref" or "out", the object array *contents* are not modified.
2. with "ref" or "out", the object array * contents" are modified.


The key point is where the keyword applies: does it apply to the object
array itself or to the elements in the array?

If you are marking the object array with "ref", it means that the array
*contents* will remain the normal behavior, which we do not care, but the
array itself may point to another array after invoking the method, please
refer to the guide below:
"Passing Arrays Using ref and out (C# Programming Guide) "
http://msdn2.microsoft.com/en-us/library/szasx730.aspx

If you are marking the elements in the array with "ref", it means the array
remains the same, but the element itself may be *modified* after invoking.
Your problem demonstrates this scenario.

Actually, your scenario is somewhat specific, which I also can not find a
definite sample or document regarding the syntax passing to
Control.Invoke. (anyway, with the help of the community, you finally got the
answer). In this scenario, I think we have to try with the existing C#
document to find out the correct syntax.

1. Based on "ref" document, "An argument passed to a ref parameter must
first be initialized.", so we should initialize the object array outside of
the method first.
2. Marking "ref" in front of the object array element is invalid(because
"ref" is only valid in method parameter), so we have no option of marking
the "ref" keyword.

With these 2 constraints, the code snippet Barry provided should be the
only valid syntax.

Hope this comment makes sense to you. If you have any concern, please feel
free to tell me. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '06 #8

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

Similar topics

0
4384
by: PJ | last post by:
Hello, I am trying to use late binding to call a COM object. I am trying to call a 'GetTables' method on the object. It's essentially a 'MetadataService' which is used to return the names of tables which are in a specific database. the first parameter to GetTables is "in only" (it's a
13
7393
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a method to an IntPtr? / Christian
2
3000
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method with arguments I need to do this as the method is being called Asynchronously from elsewhere using delegate's BeginInvoke method. The form is well...
14
7340
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 (and types), and with out parmeters. What I want to do is to support each method call with exception (http 404, soap exception, and other types of...
19
6698
by: trint | last post by:
Ok, I start my thread job: Thread t = new Thread(new ThreadStart(invoicePrintingLongRunningCodeThread)); t.IsBackground = true; t.Start(); There are lots of calls to controls and many happen in function calls from invoicePrintingLongRunningCodeThread. I need just an example in
4
7233
by: _DD | last post by:
In the switch-over to C# 2.0, I thought it would be good to get rid of external delegates for thread access to controls. In looking into anonymous methods I ran across MethodInvoker. I haven't been able to come up with code samples that show usage in this context. Current C# v1 code: public delegate void ControlUpdateDelegate(long...
2
269
by: dani kotlar | last post by:
Is it possible to pass the Invoke or BeginInvoke functions as a parameter a delegate encapsulating a function with a non empty set of parameters? How is it done?
3
5163
by: rn5a | last post by:
The ASPX FileUpload control displays a TextBox along with a 'Browse...' Button. Setting the different properties of this control just reflects the changes in the TextBox but not the Button. For e.g. if the BackColor of a FileUpload control is set to blue, then only the TextBox color changes to blue but the BackColor of the Button doesn't...
1
941
by: Just_a_fan | last post by:
I want to be able to call my thread safe routine in a more normal way but I cannot figure out the syntax or even if there is any. I have tried all variations I can think of. What I would like to code is: StatusAdd("Here is the text to display.") But what I do now is:
0
7619
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...
0
7930
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. ...
0
8138
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...
1
7681
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...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
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...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.