Why are out parmeters included in an BeginInvoke ?

J

Jon

Why are out parmeters included in an BeginInvoke? They seem to do
nothing?

TestProgam:

using System;

namespace TempConsole {
class App {
public delegate void MyDelegate( out byte b, out string s );

public static void OnMyDelegate( out byte b, out string s ) {
b = 10;
s = "Changed by Delegate.";
}

static void Main( string[] arg s) {
MyDelegate md = new MyDelegate( OnMyDelegate );
byte b1 = 1;
string s1 = "Unchanged by Delegate.";
IAsyncResult ar = md.BeginInvoke(out b1, out s1, null,
null);

ar.AsyncWaitHandle.WaitOne();

byte b2 = 2;
string s2 = "Will be changed by Delegate.";
md.EndInvoke(out b2, out s2, ar);

Console.WriteLine( "BeginInvoke out arguments: {0}, {1}",
b1, s1 );
Console.WriteLine( "EndInvoke out arguments: {0}, {1}",
b2, s2 );
Console.ReadLine();
}
}
}

Results:
BeginInvoke out arguments: 1, Unchanged by Delegate.
EndInvoke out arguments: 10, Changed by Delegate.
 
G

Guest

Hi Jon,
Although we should use out parameters for output proposes only, but
technically they can be used as ref parameters to supply inputs if necessary
but that is not recomended for some reasons (can tell if interested).

so, as long as they can supply input, they included in BeginInvoke, but the
actual modifed parameters which get the output results will be accessed via
EndInvoke.
 
J

Jon

I can not access the out parameters before OnMyDelegate initializes
them. So I can not see how they can supply input.

public static void OnMyDelegate(out byte b, out string s) {
//Console.WriteLine( "OnMyDelegate arguments: {0}, {1}", b,
s ); //This does not compile

b = 10;
s = "Changed by Delegate.";
}
 
G

Guest

I believe they could supply inputs in v1.0 because i used it as i remember,
and it is quite sure for me that supplying input for out param in v1.1 is not
useful anymore just like you discovered, but it seems that BeginInvoke still
as it was since v1.0 .

if BeginInvoke requirment of out paramters bother you, you can supply nulls
in them and get the true putput from EndInvok
 
J

Jon

I guess the rules (at least for v1.1) are:

BeginInvoke() 'out parameters' are ignored.

EndInvoke() 'ref parameters' are really output only.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top