Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0.NET4Async
Hi,
 
I'm trying to use two asynchronous methods in one method.
the methods look like this:
class Sender
{
   public static void sendMessage(Int32 uniqueRequestID, Int32 requestCode
}
 
class Receiver : IReceiver
{
   public override void onAnswerReceive(Int32 uniqueRequestID, Answer answer) 
   { 
       ... 
   }
}
 
and I'm trying to have this:
class Client
{
   private Receiver;
 
   Data getData(Int32 code)
   {
       Int32 id = this.CreateNewUniqueID();
       Sender.sendMessage(id, code);
       ....?
       return new Data(answerIGotSomehowFromReceiverOnAnswerReceive);
   }
}
 
also Receiver.onAnswerReceive is going to be called for a lot of answers , the link between the request and the answer is the Int32 uniqueRequestID.
 
So I have two problems :
1. how do I do to wait for the answer?
2. how to access the Answer from getData method in C# 4.0 ?
(I saw some articles about async/wait but as I understand it's for the next release of .NET so not usable)?
 
It must be a pretty common problem but I'm not sure what to look for?
One idea was to add a Dictionary<Int32, Answer> in the Receiver class, but if there are some .NET tools designed for this I would rather use them...
 
Any idea, link, or even code sample more than welcome.
 
Thanks in advance,
Posted 29-Jun-12 0:55am
Comments
Sandeep Mewara at 29-Jun-12 7:15am
   
5! Nicely asked, detailed question.
Sergey Alexandrovich Kryukov at 29-Jun-12 14:00pm
   
I see. I would ask you the following? Why would you consider using asynchronous approach? --SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Let me continue from the question I asked in my comment to the question.
 
I think that asynchronous API flourished when threads were not a commonplace; and these days they finally lost their purposes, except supporting some legacy. Using purely synchronous approaches with all blocking call but in separate threads is much better. You follow mostly sequential logic, you use commonly thread synchronization primitives and techniques you need to use anyway, you have much more of much clearer control. By those reasons, such multithreading code becomes more reliable, easier to test and debug.
 
—SA
  Permalink  
Comments
William2011 at 30-Jun-12 12:14pm
   
Hi, Thanks for the feedback. If I understand you correctly, my Client class follows what you says. My problem is that I have to use the Sender and IReceiver classes that are a proprietary API over which I have no control.
Sergey Alexandrovich Kryukov at 30-Jun-12 13:09pm
   
Then, if you have problems with it, it would be good to have documentation on this API... --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid