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. 4 2353
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.
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.";
}
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."; }
I guess the rules (at least for v1.1) are:
BeginInvoke() 'out parameters' are ignored.
EndInvoke() 'ref parameters' are really output only. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grandma Wilkerson |
last post by:
My question concerns the documentation for Control.BeginInvoke(). At one
point is says:
"Executes the specified delegate asynchronously with the specified
arguments, on the thread that the...
|
by: arkam |
last post by:
Hi,
I found a sample on internet :
formMain.BeginInvoke(new MyClickHandler(this.OnMyClick));
I would like to do the same but in a class library where there is no forms !
Where can I find...
|
by: David Sworder |
last post by:
Hi,
I have a form that displays data (is that vague enough for you?). The
data comes in on a thread-pool thread. Since the thread pool thread is not
the same as the UI thread, the callback...
|
by: Valerie Hough |
last post by:
I'm not entirely sure what the difference is between these two approaches.
In order to avoid reentrant code, I was using Control.BeginInvoke in my UI
to cause an asynchronous activity to be done...
|
by: john doe |
last post by:
I have a question about BeginInvoke method found on the Control class.
To quote the docs:
"Executes the specified delegate asynchronously with the specified
arguments, on the thread that the...
|
by: RWF |
last post by:
I have a windows form of which I will be saving the user's selections from
DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are collected,
I really will have no use for any of the...
|
by: Flack |
last post by:
Hello,
If I understand BeginInvoke correctly, when it is called your delegate is
run on a thread pool thread. Now, if you supplied a callback delegate, that
too is called on the same thread...
|
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...
|
by: Ben Voigt [C++ MVP] |
last post by:
As much as the CLR team assures us that it's ok to fire-and-forget
Control.BeginInvoke, it seems it isn't. Maybe this is a bug.
See for example: the comments in...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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: 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...
|
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...
|
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,...
|
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...
| |