Click here to Skip to main content
13,556,069 members
Rate this:
Please Sign up or sign in to vote.

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
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?

1 solution

Rate this: bad
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.

William2011 30-Jun-12 12:14pm

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...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100