473,806 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using AsyncResult to obtain results from delegates

Below is the example that I mercilessly copied from Jon's article. In
this example, I do not understand how his AsyncResult was able to
retrieve delegates. In other words, using AsyncResult delegateResult
= (AsyncResult) result --will it always return the active/completed
delegates in loaded in CLR?

Thanks
using System;
using System.Threadin g;
using System.Runtime. Remoting.Messag ing;

delegate int SampleDelegate( string data);

class AsyncDelegateEx ample2
{
static void Main()
{
SampleDelegate counter = new SampleDelegate( CountCharacters );
SampleDelegate parser = new SampleDelegate( Parse);

AsyncCallback callback = new AsyncCallback (DisplayResult) ;

counter.BeginIn voke ("hello", callback, "Counter returned
{0}");
parser.BeginInv oke ("10", callback, "Parser returned {0}");

Console.WriteLi ne ("Main thread continuing");

Thread.Sleep (3000);
Console.WriteLi ne ("Done");
}

static void DisplayResult(I AsyncResult result)
{
string format = (string) result.AsyncSta te;
AsyncResult delegateResult = (AsyncResult) result;
SampleDelegate delegateInstanc e =
(SampleDelegate )delegateResult .AsyncDelegate;

Console.WriteLi ne (format,
delegateInstanc e.EndInvoke(res ult));
}

static int CountCharacters (string text)
{
Thread.Sleep (2000);
Console.WriteLi ne ("Counting characters in {0}", text);
return text.Length;
}

static int Parse (string text)
{
Thread.Sleep (100);
Console.WriteLi ne ("Parsing text {0}", text);
return int.Parse(text) ;
}
}
Oct 5 '08 #1
1 3130
On Sun, 05 Oct 2008 14:04:43 -0700, puzzlecracker <ir*********@gm ail.com>
wrote:
Below is the example that I mercilessly copied from Jon's article. In
this example, I do not understand how his AsyncResult was able to
retrieve delegates. In other words, using AsyncResult delegateResult
= (AsyncResult) result --will it always return the active/completed
delegates in loaded in CLR?
The AsyncResult isn't special. It's just a class that stores state
related to the asynchronous invocations of the delegates used in the
examples. In particular, the AsyncDelegate simply returns the reference
to the delegate that was used in the call to BeginInvoke() in the first
place.

Because that delegate instance is the one that created the AsyncResult
instance, it's trivial for that reference to be included in the
AsyncResult instance.

Note, by the way, that there's no such thing as "the active/completed
delegates in loaded in CLR". Whatever you meant by that, a particular
delegate is neither active nor inactive, nor is it completed or
incomplete. You can call BeginInvoke() multiple times on the same
delegate. It's not the delegate that's "active" or "completed" , but
rather the specific asynchronous invocation of the delegate.

For example, if you had this code:

void Method()
{
Action action = delegate { Thread.Sleep(50 00); };

action.BeginInv oke(Callback, "action 1");
Thread.Sleep(25 00);
action.BeginInv oke(Callback, "action 2");
}

void Callback(IAsync Result result)
{
Console.WriteLi ne("finished delegate {0}", result.AsyncSta te);
}

Then after Callback is called the first time, would you consider the
delegate "active" or would you consider it "completed" ? The delegate is
"completed" in the sense that the callback was called, but it's "active"
in the sense that there's still an async invocation of the delegate that
hasn't finished yet.

In other words, the words "active" and "completed" don't describe the
delegate at all.

Pete
Oct 5 '08 #2

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

Similar topics

7
21320
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of someone (client) that visits a web site through an anonymous proxy if this person ONLY has JavaScript enabled in their browser? This is NOT a question about PHP, perl, VBScript, Java(.class), or ActiveX. Let us _only_ deal with JavaScript for...
0
2743
by: Nelson Russa | last post by:
Hi, i need to get some info from a website page that requires an certificate. Ive got the provided certificate installed in IE, and when accessing the website page, it shows a window to select the client certificate and then shows the page correctly. Im trying to do this by code (C# aspnet), using webrequest.
0
2317
by: Peter Conrey | last post by:
I have a perl web service (using SOAP::Lite) with a method called "Detail" that returns a strucure (hash reference to be exact). It works fine when consumed by a Perl client, but when I try to consume it with a C# application, I get the following runtime error from C#: Cannot assign object of type System.Xml.XmlNode to an object of type ConsoleApplication1.com.hilton.crmdev.SummaryType. Below is the Perl code that generates the hash,...
3
6630
by: JL | last post by:
I have a VB.NET desktop program that reads/writes data to a server using a Java-based Web Service. This web service, in identical formats, is located on several servers with each server being a different customer and different data content. To connect with the Java Web Service, I downloaded the WSDL file and used wsdl.exe to create a .vb class file. In my program, I simply call the functions of the class to communicate with the server. ...
0
2409
by: Fred Herring | last post by:
I currently use httpwebrequest to download large byte array files from my server. I am curious about how to change my httpwebrequest code to allow posing a large byte array to a virtual foldeer. This is my download code which work well in returning a large byte array from the server: Function GetImageFromURL(ByVal url As String) As Byte() allDone.Reset() Try Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url),...
12
1675
by: The One We Call 'Dave' | last post by:
Hi, Various custom controls in my WinForms application register for the 'Application.Idle' event on load, and unregister on dispose. To avoid memory leaks, it's essential that I remember to unregister on dispose. Before my application exits, I'd like to do an 'assert' to assure myself that there are no cases in which I've forgotten to unregister. I was under the impression that an event is nothing more than a collection of delegates....
2
1817
by: Robbert van Geldrop | last post by:
Hello, I have a client that consumes a WebService using a WSE 2.0 sp3 interface. I use a WebProxy as generated by VS.Net, based on a webService URL. Occasionaly when debugging I get an unhandled expection in disassembly code: "Connection was forcidbly closed by remote host" stack trace:
3
1601
by: Sidanalism | last post by:
I miss the old way when asynchronous calls are driven my window messages. Yes a thread pool might greatly improve application performance but there are time I don't want my event to be fire in a thread differ from the calling thread. When a delegate is called in another thread it could be frustrating for I have to consider thread occurrence issues in all the codes it may invoke along my event chain. Sure the "Control.BeginInvoke()" could...
0
2213
by: tccode97 | last post by:
To whom it may concern, I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link http://msdn2.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx First of all, thanks for microsoft team for this post.
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10624
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
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.