Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:03:22+00:00 2026-06-10T19:03:22+00:00

I have such code: public void IssueOrders(List<OrderAction> actions) { foreach (var action in actions)

  • 0

I have such code:

    public void IssueOrders(List<OrderAction> actions)
    {
        foreach (var action in actions)
        {
            if (action is AddOrder)
            {
                uint userId = apiTransactions.PlaceOrder((action as AddOrder).order);
                Console.WriteLine("order is placing userId = " + userId);
            }
            // TODO: implement other actions
        }
        // how to wait until OnApiTransactionsDataMessageReceived for all userId is received?

        // TODO: need to update actions with received data here
    }

    private void OnApiTransactionsDataMessageReceived(object sender, DataMessageReceivedEventArgs e)
    {
        var dataMsg = e.message;
        var userId = dataMsg.UserId;

apiTransactions.PlaceOrder is asynchronous so I receive userId as result but I will receive data in callback OnApiTransactionsDataMessageReceived.

So for example If I place 3 orders, i will receive 3 userId, for example 1, 3, and 4. Now I need to wait until data for all these userId is received.

userId is always increasing if this is important. This is almost integer numbers sequence, but some numbers may be ommited due parallel execution.

UPD Note:

  • IssueOrders can be executed parallel from different threads
  • callack may be called BEFORE PlaceOrder returns

UPD2

Likely I need to refactor PlaceOrder code below so I can guarantee that userId is known before “callback” is received:

    public uint PlaceOrder(Order order)
    {
        Publisher pub = GetPublisher();

        SchemeDesc schemeDesc = pub.Scheme;
        MessageDesc messageDesc = schemeDesc.Messages[0]; //AddMM
        FieldDesc fieldDesc = messageDesc.Fields[3];
        Message sendMessage = pub.NewMessage(MessageKeyType.KeyName, "FutAddOrder");

        DataMessage smsg = (DataMessage)sendMessage;
        uint userId = counter.Next();
        FillDataMessageWithPlaceOrder(smsg, order, userId);

        System.Console.WriteLine("posting message dump: {0}", sendMessage);
        pub.Post(sendMessage, PublishFlag.NeedReply);
        sendMessage.Dispose();

        return userId;
    }

So I need to split PlaceOrder to two methods: userId CreateOrder and void PostOrder. This will guarantee that when callback is received I know userId.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:03:24+00:00Added an answer on June 10, 2026 at 7:03 pm

    One of the most silly and working approaches would be:

    public void IssueOrders(List<OrderAction> actions)
    {
        var userIds = new List<uint>();
        lock(theHashMap)
            theHashMap[userIds] = "blargh";
    
        foreach (var action in actions)
        {
            if (action is AddOrder)
            {
                lock(userIds)
                {
                   uint userId = apiTransactions.PlaceOrder((action as AddOrder).order);
                   Console.WriteLine("order is placing userId = " + userId);
    
                   userIds.Add(userId);
                }
            }
    
            // TODO: implement other actions
        }
    
        // waiting:
        do
        {
           lock(userIds)
              if(userIds.Count == 0)
                 break;
    
           Thread.Sleep(???); // adjust the time depending on how long you wait for a callback on average
    
        }while(true);
    
        lock(theHashMap)
            theHashMap.Remove(userIds);
    
        // now you have the guarantee that all were received
    }
    
    private Dictionary<List<uint>, string> theHashMap = new Dictionary<List<uint>,string>();
    
    private void OnApiTransactionsDataMessageReceived(object sender, DataMessageReceivedEventArgs e)
    {
        var dataMsg = e.message;
        var userId = dataMsg.UserId;
    
        // do some other things
    
        lock(theHashMap)
            foreach(var list in theHashMap.Keys)
               lock(list)
                  if(list.Remove(userId))
                     break;
    }
    

    but, this is quite crude approach.. Its hard to suggest anything more unless you explain what do yo umean by wait – as Jon asked in the comments. For example, you may might want to leave the IssueOrders, wait anywhere, and just be sure that the some extra job is done when all have arrived? Or maybe you cannot leave the IssueOrders unless all are received? etc..

    Edit: please note that near ADD, the lock must be before PlaceOrder, or else, when the callback arrive hyper-fast, the callback may attempt to remove the ID before it is added. Also, note that this implementation is very naiive: the callback must search and lock through all the lists at each time. With a few additional dictionary/maps/indexes, it may be optimized much, but I did not do that here for readability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if i have got such java code: public static void main(String[] args) { for(int
I have code such as: public void MethodA() { MyManualResetEvent.Reset(); } public void MethodB()
I have putting such data into LinearView. as like below code: public void showResult()
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
i have such code var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true); if (prj
I have just encountered this following code: public class TestFinally { public static void
I have the following code: public void Test(IMyInterface iInterface) { iInterface.CallMethod ( ); }
Assume we have such code: public class Observer { public event EventHandler X =
I have a thread that executes the following code: public void run() { try
how can i make such code working? public void start() { ThreadPoolManager.getInstance().scheduleWithFixedDelay(new Thread(), 1000,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.