Click here to Skip to main content
15,891,739 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I'm trying to use two asynchronous methods in one method.
the methods look like this:
C#
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:
C#
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
Comments
Sandeep Mewara 29-Jun-12 7:15am    
5!Nicely asked, detailed question.
Sergey Alexandrovich Kryukov 29-Jun-12 14:00pm    
I see. I would ask you the following? Why would you consider using asynchronous approach?
--SA

1 solution

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
 
Share this answer
 
Comments
William2011 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900