iasyncresult

What are the strengths of the IAsyncResult pattern? [closed]

送分小仙女□ 提交于 2021-02-08 06:19:38
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I feel that many classes (e.g. TcpClient , UdpClient , HttpListener ) would've been much easier to understand and use if they were

Blocking until an event completes

独自空忆成欢 提交于 2020-12-04 04:04:49
问题 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 { Interlocked.Increment(ref completed); }; obj.StartWork(); // Busy loop while (completed == 0) Thread.Sleep(50); // StartWork() has completed at this point. return obj

Blocking until an event completes

南楼画角 提交于 2020-12-04 04:04:12
问题 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 { Interlocked.Increment(ref completed); }; obj.StartWork(); // Busy loop while (completed == 0) Thread.Sleep(50); // StartWork() has completed at this point. return obj

Can I use a single instance of a delegate to start multiple Asynchronous Requests?

倖福魔咒の 提交于 2020-01-04 19:02:41
问题 Just wondered if someone could clarify the use of BeginInvoke on an instance of some delegate when you want to make multiple asynchronous calls since the MSDN documentation doesn't really cover/mention this at all. What I want to do is something like the following: MyDelegate d = new MyDelegate(this.TargetMethod); List<IAsyncResult> results = new List<IAsyncResult>(); //Start multiple asynchronous calls for (int i = 0; i < 4; i++) { results.Add(d.BeginInvoke(someParams, null, null)); } //Wait

How to make a generic delegate EndInvoke?

China☆狼群 提交于 2019-12-26 18:12:13
问题 So I have the following: private delegate Foo1 GetFooAsync1(Foo1 foo1); private delegate Foo2 GetFooAsync2(Foo2 foo2); private delegate Foo3 GetFooAsync3(Foo3 foo3); private delegate Foo4 GetFooAsync4(Foo4 foo4); private FooAsync1 foo1; private FooAsync2 foo2; private FooAsync3 foo3; private FooAsync4 foo4; And the lists goes on and on, then inside a method I don't want to put a try catch on each EndInvoke, because sometimes it does throw an exception but it shouldn't stop the system, and

How to create an IAsyncResult that immediately completes?

这一生的挚爱 提交于 2019-12-23 07:43:57
问题 I am implementing an interface which requires implementations of BeginDoSomething and EndDoSomething methods. However my DoSomething isn't really long-running. For simplicity assume DoSomething only compares two variables and return whether a > b So my BeginDoSomething should be like: protected override IAsyncResult BeginDoSomething(int a, int b, AsyncCallback callback, object state) { bool returnValue = a > b; return ...; //what should I return here? //The method actually already completed

C# IAsyncResult WaitAll

余生颓废 提交于 2019-12-23 03:51:46
问题 In some of the implementations of WaitAll I have seen the following code IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null) IAsyncResult result2 = Method.BeginInvoke(20, MyCallback, null) WaitHandle[] waitHandles = new WaitHandle[] { result1.AsyncWaitHandle, result2.AsyncWaitHandle}; WaitHandle.WaitAll(waitHandles) Does this seem right ? What are the chances that before the waitHandles array is created one of the calls complete ? Regards, Dhananjay 回答1: Makes sense to me. //

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

孤人 提交于 2019-12-20 03:21:59
问题 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 is the receive code: Private Sub ReceiveCallback(ByVal ar As IAsyncResult) Try ' Retrieve the state object and the client socket from the asynchronous state

Exception, but only on 3rd time - The IAsyncResult object was not returned from the corresponding asynchronous method on this class

家住魔仙堡 提交于 2019-12-13 02:14:46
问题 I have a legacy (2008) Windows service application (using System.ServiceProcess.ServiceBase) that I need to utilize in a slightly different way to how it's working now. At the moment it starts up and creates a TCPLISTENER on a specific port, for a client app to connect to (which it does once) in order to send/receive requests to the listening port. This all works fine. However, the adaptation requires use of a web app to connect to the listener port, then send/rcv as normal but after the

Too many arguments in BeginXXX for FromAsync?

蓝咒 提交于 2019-12-09 08:13:53
问题 I have an async method with the following signature: IAsyncResult BeginGetMyNumber(string foo, string bar, string bat, int bam, AsyncCallback callback, object state) I want to execute it using Factory.FromAsync like this: var result = Task<int>.Factory.FromAsync( instance.BeginGetMyNumber, instance.EndGetMyNumber, "foo", "bar", "bat", 100, /*bam*/ null); but I get the following error: Argument 1: cannot convert from 'method group' to 'System.Func' It seems there is no suitable overloaded