By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Why are out parmeters included in an BeginInvoke ?

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
Ahmed Ghozzy
"Jon" wrote:
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.

Nov 17 '05 #2

P: n/a
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.";
}

Nov 17 '05 #3

P: n/a
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
--
Ahmed Ghozzy
"Jon" wrote:
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.";
}

Nov 17 '05 #4

P: n/a
Jon
I guess the rules (at least for v1.1) are:

BeginInvoke() 'out parameters' are ignored.

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

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.