iasyncresult

Implementing IAsyncResult explicitly

I am generally wary of implementing interfaces partially. However, IAsyncResult is a bit of a special case, given that it supports several quite different usage patterns. How often do you use/see used the AsyncState/AsyncCallback pattern, as opposed to just calling EndInvoke, using AsyncWaitHandle, or polling IsCompleted (yuck)? Related...

Using BeginInvoke/EndInvoke in a multithreaded fashion. How do AsyncCallback, AsyncWaitHandle and IsCompleted interact?

Andreas Huber's answer to this question gave me an idea to implement Concurrent<T> with async delegates instead of the ThreadPool. However, I am finding it harder to understand what's going on when an AsyncCallback is passed to BeginInvoke, especially when several threads have access to IAsyncResult. Unfortunately, this case doesn't seem...

C#, IAsyncResult and the thread pool

I use the Action<object>.BeginInvoke() method, does this use the thread pool or not? I have the following C# code: List<FileHash> hashList1 = hashList.Where((x, ind) => ind % 2 == 0).ToList(); List<FileHash> hashList2 = hashList.Where((x, ind) => ind % 2 == 1).ToList(); Action<object> oddWork = CalcHash; Action<object...

How can I easily chain two asynchronous requests together?

I've got some code that screen scrapes a website (for illustrative purposes only!) public System.Drawing.Image GetDilbert() { var dilbertUrl = new Uri(@"http://dilbert.com"); var request = WebRequest.CreateDefault(dilbertUrl); string html; using (var webResponse = request.GetResponse()) using (var receiveStream = webResponse...

What is a proper implementation of the IAsyncResult interface?

I'm looking into adding some flexibility to a class that I've created which establishes a connection to a remote host and then performs an exchange of information (a handshake). The current implementation provides a Connect function which establishes the connection and then blocks waiting on a ManualResetEvent untill the two parties have...

Genericising delegate/IAsyncResult calls

I have a WCF web service that currently searches multiple, hard-coded dtSearch indices and then merges the resulting datasets to be returned back to the client. I have the following C# code: public class Search : ISearch { delegate DataTable PDelegate(string term, int cid); delegate DataTable CDelegate(string term, int sid); ...

AsyncWaitHandle to terminate 3rd party API properly implemented?

Hi, "session.identify" is a third party COM API that I call and have no access to. It performs a server query that, somehow, locks up occasionally (and thus halts the main program which is waiting for the result). My attempt was to wrap it in an AsyncDelegate so I would be able to give it a timeout and after expiration of the timeout ...

Synchronous and asynchronous callbacks

I get confused with some terms while reading MSDN documents and code samples. What are callbacks in C#? In particular, what are synchronous and asynchronous callbacks ? Please explain these from a layman's point of view. Also, please explain the IAsyncResult interface. How can we implement it? (with very simple example) Thanks in adv...

Async HttpWebRequest - thread number

Let's say I have the following code : ThreadPool.SetMinThreads(100, 100); for (int i = 0; i < 100; i++) { var request = WebRequest.Create(url); request.BeginGetResponse(ar => { //inside AsynchCallBack method for request.BeginGetResponse() var response = (HttpWebResponse)request.EndGetResponse(ar); string html; using (var ...

Blocking until an event completes

How can you block until an asynchronous event completes? Here is a way to block until the event is called by setting a flag in the event handler and polling the flag: private object DoAsynchronousCallSynchronously() { int completed = 0; AsynchronousObject obj = new AsynchronousObject(); obj.OnCompletedCallback += delegate {...

VB.NET 3.5 SocketException on deployment but not on development machine

I have written an async UDP client to talk to a server at my company. When I run on my developer machine all is well. When I deploy to another machine I get a socket exception on EndReceive the first time I send data over the socket. My dev box is Win7 and I have deployed to both an XP SP3 machine and a Server 2003 R2 machine. Below ...

What are pros and cons of consuming web services with 1. events / 2. IAsyncResult?

I made a WPF example that consumes a web service (www.webservicex.com/globalweather.asmx) in two different ways: with events like this: public Window1() { InitializeComponent(); DataContext = this; Location = "loading..."; Temperature = "loading..."; RelativeHumidity = "loading..."; client.GetWeatherCompleted ...

Converting Asynchronous Programming Model (Begin/End methods) into event-based asynchronous model?

Let's say I have code that uses the Asynchronous Programming Model, i.e. it provides the following methods as a group which can be used synchronously or asynchronously: public MethodResult Operation(<method params>); public IAsyncResult BeginOperation(<method params>, AsyncCallback callback, object state); public MethodResult EndOperat...

Help threading HttpWebRquest in c#

Hi guys just wondering if somebody could help me try and correctly thread my application, I am constantly hitting an hurdle after another, I have never been to clued up on threading in applications. I have tryed following this http://www.developerfusion.com/code/4654/asynchronous-httpwebrequest/ tutorial. basically I'm just trying to st...

Reactive Extensions (Rx) and asynchronous class

I've read in this post: "The joy of Rx: The event-based asynchronous pattern vs IObservable" that the use of EBAP is discourage. What is the best way to design an asynchronous component with the new Rx extensions (something like the PrimeNumberCalculator example of msdn)? Thank you in advance. Update I managed to write my own prime nu...

C# void ReceiveData(IAsyncResult iar)

i would like some help with the following. this is to do with Asynchronous sockets. from the sender: string stringData = "Welcome to my server server server"; byte[] message1 = Encoding.ASCII.GetBytes(stringData); //MessageBox.Show(message1.Length.ToString()); client.BeginSend(message1, 0, message1.Length, SocketFlags.None, new Async...

Set a timeout and get a result from a delegate thread

I've never had much to do with async callbacks and delegates before on WinForms (i.e., not AJAX) so I'm hoping someone can help me out with what I'm trying to do here. I'm logging into a third-party service over the web. The Login() method is of type Void. Because this can take up to 10 seconds (very occasionally longer) I want to do t...

Cleanly handling AsyncTimeout on ASP.NET Async Page

According to this article The Begin Event Handler is Always Invoked The second impliciation of AsyncTimeout that I had not really internalized until recently is that the begin event handler for a registered async task is always invoked, even if the page has timed out before ASP.NET gets around to starting that task...

problem withAsync SqlComman

I have problem with Timeout, when I run a command through app, a timeout exception is thrown, but when I run it directly in sql there is no timeout exception! my SP take about 11 min when I run it directly. for solving this issue, I found below code here, but It doesn't work properly! Immediately after beginExecute, IAsyncResult.iscompl...

Why doesn't my NamedPipeServerStream wait??

I'm working with a NamedPipeServerStream to communicate between two processes. Here is the code where I initialize and connect the pipe: void Foo(IHasData objectProvider) { Stream stream = objectProvider.GetData(); if (stream.Length > 0) { using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("Visualiz...