Click here to Skip to main content
15,887,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call

(1) GetMessages() this will keep bringing the messages in separate thread

(2) GetOrderData() this will fetch the order details and insert them in datagrid and the its task is finished here

(3) UpdateOrderBook() this will now update the existing datagrid (which already has some data from step 2). Here in this method I want to check the queue and have to iterate the concurrentqueue then do some processing and then insert that record in the existing grid.

The two process (1,3) would run asynchronously but for the first time it should process in above order.

I got stuck in making them run asynchronously.


This works perfectly for the first time. But when I change the product and reconnect the things mess up and the data is not processed properly.

I am new to multi-threading so not sure if I need to use lock or async/await or something else. What am I doing wrong in the above code?

What I have tried:

C#
void OnReceivingFeedMessage(message)
    {
         concurrentQueue.Enqueue(message);
      
         if (!messageStreamStarted)   // only first time get order book
         {
             messageStreamStarted = true;
             GetOrderBookData();
         }
    }

    private void GetOrderBookData()
    {
        MarketData m = new MarketData();
        ProductOrderBook p = m.GetProductOrderBook(productId);           
        bidsList = p.bids;
        asksList = p.asks;
        isOrderBookUpdated = true;

        Task task3 = Task.Run(() => KickStartToProcessQueue());         
    }

    private void KickStartToProcessQueue()
    {
        while (threadProcessQueueExist)
        {
            int recordCountNew = concurrentQueue.Count();
            if (recordCountNew != 0)
            {
                if (isOrderBookUpdated)
                {
                    ProcessQueueMessages();
                }
            }
        }
    }

    private void ProcessQueueMessages()
    {
        if (!concurrentQueue.IsEmpty)
        {
            string jsonString;
            while (concurrentQueue.TryDequeue(out jsonString))
            {
              // have to insert the record in existing order book
            }
         }
    }

// The code written on product selectedindex change

    private void CloseAndReconnectToGetWebsocketFeed()
    { 
        w.CloseWebsocketConnection();                 
        messageStreamStarted = false;            
        isOrderBookUpdated = false;
        ConcurrentQueue<string> wssMessagesQueue = new ConcurrentQueue<string>();
        concurrentQueue = wssMessagesQueue;

        ConnectAndGetWebsocketFeedMessages(); // this calls OnReceivingFeedMessage
    }
Posted
Updated 30-Nov-17 8:36am
v3

 
Share this answer
 
Comments
Richard Deeming 1-Dec-17 13:46pm    
Resurrecting a question that was asked and answered six months ago just to promote your own articles?

Yeah, that's abuse.

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